Melvin wrote:
Hi I have a file in the following format
111
222
333
Now I need to print the following output from the given input file as
111 222 333
$ echo "111
222
333" | perl -l040pe1
111 222 333
Is there a way I can do this in perl?
I tried 2 ways (both ere essentially the same)
1) Parsing the file and pushing the inputs to a string array. However
since the inputs had a newline, I could n't remove them
The chomp() function is usually used to remove newlines at the end of a
line.
perldoc -f chomp
2) Using the .="" operator to concatenate. Here too, the newlines from
the file were taken.
my $parent_loop;
my $line_cnt;
my @lines;
my @output;
open (FILE_PATTERN ,$ARGV[0]) || die ("ERROR: NO INPUT, NO OUTPUT
hahaha");
while (<FILE_PATTERN>) {
push @lines, $_;
$line_cnt++;
}
close (FILE_AUDIO);
for ($parent_loop=0; $parent_loop< $line_cnt; $parent_loop++) {
push @output,"$lines[$parent_loop]";
}
for ($parent_loop=0; $parent_loop<= $line_cnt; $parent_loop++) {
print $urg_command[$parent_loop];
}
Could someone let me know how I can remove the newline?
while ( <FILE_PATTERN> ) {
chomp;
print "$_ ";
}
Or perhaps:
while ( <FILE_PATTERN> ) {
s/\n/ /;
print;
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/