in reply to reading lines into an array

How about:

chomp( my @lines = <$infile> );

Or...

use File::Slurp; my @lines = read_file( $infile, chomp => 1 );

There's more than one way to do it. Assuming the file doesn't grow to swamp available memory, they all work. I'd say that the File::Slurp method, and the "chomp( my @lines..." method are probably the easiest to look at and immediately know what's happening.

Any method that ends up slurping the file into an array will make $line_count useless after the slurp is done, since you can always say: my $line_count = scalar @lines;

Update: I just noticed that your map method has a couple of bugs (which is one compelling reason to keep it as simple as possible). This:

my @lines = map { chomp; $_; $line_count++; } <$infile>;

... chomps $_, then referrs to $_ for no reason, then increments $line_count, then pushes each line's line_count onto @lines. In the end, @lines will contain "( 0, 1, 2, 3, 4, 5, ... )". You probably meant to write it like this:

my @lines = map { chomp; $_ } <$infile>; my $line_count = scalar @lines;

Dave

Replies are listed 'Best First'.
Re^2: reading lines into an array
by fionbarr (Friar) on Jun 10, 2013 at 20:12 UTC
    thanks...your example #1 is my new idiom