Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.


Comment on Re^2: Reading files n lines a time
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

    or

     last if not defined $line0||$line1||$line2||$line3
Re^3: Reading files n lines a time
by choroba (Abbot) on Dec 06, 2012 at 14:17 UTC
    Using
    while(( my @lines = map $_ = <>, 1 .. 4 )[-1]) {
    should stop if the last line is not read as well. (You might need to add defined.)
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007564]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-08-31 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls