Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Reading files n lines a time

by naturalsciences (Beadle)
on Dec 06, 2012 at 13:29 UTC ( #1007564=note: print w/replies, xml ) Need Help??

in reply to Re: Reading files n lines a time
in thread Reading files n lines a time

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.

Replies are listed 'Best First'.
Re^3: Reading files n lines a time
by choroba (Bishop) on Dec 06, 2012 at 14:17 UTC
    while(( my @lines = map $_ = <>, 1 .. 4 )[-1]) {
    should stop if the last line is not read as well. (You might need to add defined.)
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Reading files n lines a time
by naturalsciences (Beadle) on Dec 06, 2012 at 14:12 UTC
    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007564]
[Corion]: I find the process quite similar to (in Prolog) passing around an accumulator variable in recursive predicates
[1nickt]: Good morning, brainy monks!
[marto]: Hi 1nickt, I know it's early days, but how is the new job?
[Corion]: Hi 1nickt!
[Corion]: Ohai marto!
[marto]: hey Corion

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2017-12-11 11:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (289 votes). Check out past polls.