Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

reading lines into an array

by fionbarr (Friar)
on Jun 10, 2013 at 19:55 UTC ( [id://1038146]=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I've always done this:
while (<$infile>) { chomp; $lines[$line_count] = $_; $line_count++; } close $infile;
but it looks a lot cooler to do this:
my @lines = map{chomp;$_;$line_count++;} <$infile>;
besides the 'cool' factor, any issues?

Replies are listed 'Best First'.
Re: reading lines into an array
by davido (Cardinal) on Jun 10, 2013 at 20:00 UTC

    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

      thanks...your example #1 is my new idiom
Re: reading lines into an array
by BrowserUk (Patriarch) on Jun 11, 2013 at 03:42 UTC

    If performance is in any way a criteria, don't use File::Slurp. And if the size of the file is any more than a few hundred lines, don't use list assignment either:

    #! perl -slw use strict; use File::Slurp; use Benchmark qw[ cmpthese ]; print `wc -l $ARGV[ 0 ]`; open our $infile, '<', $ARGV[ 0 ] or die $!; our $I //= -1; cmpthese $I, { a=> q[ sysseek $infile, 0, 0; my @lines = read_file( $infile, chomp => 1 ); print 'a: ', scalar @lines if $I == 1; ], b=> q[ sysseek $infile, 0, 0; chomp( my @lines = <$infile> ); print 'b: ', scalar @lines if $I == 1; ], c=> q[ sysseek $infile, 0, 0; my @lines; $lines[ @lines ] = <$infile> until eof $infile; chomp @lines; print 'c: ', scalar @lines if $I == 1; ], }; __END__ C:\test>t-slurp -I=1 small.tsv 1000000 small.tsv a: 1000000 (warning: too few iterations for a reliable count) b: 1000000 (warning: too few iterations for a reliable count) c: 1000000 (warning: too few iterations for a reliable count) s/iter a b c a 257 -- -98% -98% b 4.94 5115% -- -16% c 4.14 6119% 19% -- C:\test>t-slurp -I=-1 small.tsv 1000000 small.tsv (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter b a c b 4.88 -- -4% -100% a 4.69 4% -- -100% c 1.39e-006 350525381% 337007680% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      thanks all; thoughtful replies.
Re: reading lines into an array
by vinoth.ree (Monsignor) on Jun 11, 2013 at 03:24 UTC

    davido ++

    use File::Slurp; my @lines = read_file("filename", chomp => 1); # will remove newline f +rom each line
    This is the easiest method to each file content into array. If you need some validation for each line you can use grep in front of read_file.

    More on reading file content into array,
    How can we read each line from a file onto an array?


    All is well

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1038146]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found