Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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 (Chancellor) 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]
[stevieb]: choroba my GPS home device is done, at least prototype v1 is. I'm just making sure all of my code does what it says now
[stevieb]: I test/prototype code on my Pi, then I migrate it over to Arduino when it's ready (if that's a better platform for what I'm doing). The whole RPi stuff was for learning at first, then it snowballed. I'm just trying to get my in-house CI...
[stevieb]: ...done, which obviously requires a specific hardware setup.
[stevieb]: I don't write tests for the lower-level distributions (ie. ICs, sensors etc) within those distributions, I have all tests in a master distribution which encompasess all of the sub-modules

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-06-25 22:58 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.