Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Reading files n lines a time

by choroba (Bishop)
on Dec 06, 2012 at 13:16 UTC ( #1007562=note: print w/replies, xml ) Need Help??

in reply to Reading files n lines a time

wont work anymore
What happens? What error message do you get?

I experimented with reading multiple lines through map, this works for me, but is a bit ugly. Maybe someone else knows a cleaner way?

while(( my @lines = map $_ = <>, 1 .. 4 )[0]) { print @lines; print "\n"; }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Reading files n lines a time
by tobyink (Abbot) on Dec 06, 2012 at 14:22 UTC

    Perhaps marginally nicer, the kite operator:

    while(( my @lines = map ~~<>, 1 .. 4 )[0]) { print @lines; print "\n"; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Considering the OP deals with multi-GB files I'd prefer the slightly uglier assignment to $_ because the kite's tail is not optimized away so all the strings would actually be shoved through the binary negation twice.

      I was surprised by the result BTW when I looked at the optree; I'm almost completely clueless about what Perl can and cannot optimize but it even a fairly trivial peephole optimizer as in early C compilers could catch this.

Re^2: Reading files n lines a time
by naturalsciences (Beadle) on Dec 06, 2012 at 13:29 UTC

    Hmm looked through my test script and yes it seems that the while(<>){$nextline=<>;$thirdline=<>} can actually be extended indefinetly. It is ugly but gets the job done.

    Why my scripts crashed seems to be of the undefined variables you get when your text files line number is not divisible by the number of your $nextlines. Then in the end you will get undefined variables for your $nextlines

    I need to get some control element that would terminate the script (or loop if I want to continiue with script) nicely. I think akin to - if any variables undefined exit loop, in the beginning of the loop. Should stop it from panicking with unfavorable Eof situation.

      while(( my @lines = map $_ = <>, 1 .. 4 )[-1]) {
      should stop if the last line is not read as well. (You might need to add defined.)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      More on the undefined variables problem. Would adding something like this do a loop be correct.
      while ($line0=<>) {$line1=<>;$line2=<>;$line3=<>; last if not defined $line0; last if not defined $line1; last if not defined $line2; last if not defined $line3; do_some_stuff; }

      Could I use some OR statements to get these last if-s on a single line. Or can or and and statements used only between two values. I was thinking like

       last if not defined $line0or$line1or$line2or$line3


       last if not defined $line0||$line1||$line2||$line3
Re^2: Reading files n lines a time
by DrWhy (Chaplain) on Dec 06, 2012 at 19:25 UTC
    Don't you need to check for defined rather than true for the first element of your array in the while loop?


    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      Probably yes. I indicated that deeper in a reply.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007562]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2018-04-25 13:24 GMT
Find Nodes?
    Voting Booth?