Actually you only need to do this:
This evaluated <FILE1> in list-context, and thus the whole file will be put in the @lines-array, one entry per line (or whatever you have assigned to $/)open(FILE1, $ARGV[0]) || die "Error: $!\n"; @lines = <FILE1>;
So then, why didn't your code work as you expected ? Well - the line
assigns the variable $_ to the array @lines. You are declaring the contents of @lines each and every time through that loop. It's almost the same with variables:@lines = $_;
this also redeclares the contents of $foo every time it is evaluated. With scalars we usually use the . (dot) operator or string concat. operator to avoid redeclaring its content:$foo = $_;
or:$foo .= $_;
So, with arrays we have something similar:$foo = $foo . $_;
That will add to @lines instead of redeclaring its contents.push @lines, $_;
Autark
In reply to RE: Parsing a file one line at a time
by autark
in thread Parsing a file one line at a time
by cburns
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |