Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Reading files n lines a time

by MidLifeXis (Monsignor)
on Dec 06, 2012 at 13:40 UTC ( #1007569=note: print w/replies, xml ) Need Help??

in reply to Reading files n lines a time

sub readNLines { my $lines_to_read = shift or die '....'; my @lines_read; while(<>) { push( @lines_read, $_ ); last if @lines_read == $lines_to_read; } if ( @lines_read != $lines_to_read ) { # error condition } return @lines_read; } while (@lines = readNLines(4)) { do_stuff() }

I might even consider the question "What am I reading from the file?" - if it is a record, then I might instead change readNLines to readRecord (as well as the surrounding usage of the data). Then the concept is abstract, and if the format of a record changes, you modify the reading piece. In my opinion,

while ( my $record = readRecord( ... ) ) { ... }
tells me much more in the context that I am concerned with than
while ( $line1=<> && $line2=<> && $line3=<>... ) { ... }

Just my $0.02.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007569]
[zentara]: Y succeeds X then preceeds Z . There is a deep meaning in that :-)
[zentara]: unicorns have a Z chromosome
shmem lives in a city which has the unicorn in its emblem
[LanX]: Dortmund !

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2017-03-30 16:57 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (362 votes). Check out past polls.