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


In reply to Re: reading lines into an array by davido
in thread reading lines into an array by fionbarr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.