Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Grabbing lines three-by-three from a file

by tadman (Prior)
on Dec 11, 2001 at 00:42 UTC ( [id://130770]=note: print w/replies, xml ) Need Help??


in reply to Grabbing lines three-by-three from a file

You could always haul the entire file in and chunk it out, or you could just re-work how you're using Perl:
#!/usr/bin/perl -w use strict; use warnings; my @three; # Stoke it chomp ($three[0] = <DATA>); chomp ($three[1] = <DATA>); while(!eof(DATA)) { chomp ($three[2] = <DATA>); # Do stuff to process @three print join ("\t", @three), "\n"; # Rotate shift(@three); } __DATA__ 1 2 3 4 5 6
This is simply using the shift function to rotate the array as you insert new data. Some notes on your implementation:
  • Don't put any "warm up" code inside the loop, put it before the loop. This eliminates the CALL sub-loop, and any associated ambiguity with last.
  • Don't name your loops for no reason. In this case, you are using last to escape a single loop, not several in a single call, so it is redundant.
  • You're backtracking within the file using seek, which seems a little overkill, since you already have the data in @three. Shuffle it around using array methods instead of reading it again and again needlessly.
  • Your use of the ?: operator is, while valid, kind of unfortunate. It is normally used in an inline capacity, such as where there is no room for an if. While it might take more room, using if will make it a lot clearer what you're doing.
Still, points for 'use warnings' and 'use strict'.

Replies are listed 'Best First'.
Re: Re: Grabbing lines three-by-three from a file
by runrig (Abbot) on Dec 11, 2001 at 00:59 UTC
    Don't name your loops for no reason.

    IMHO, there's nothing wrong with naming your loops, especially (but not only) when there are multiple or nested loops in the general vicinity. It just makes things more clear, and more self-documenting.

      The last function, by definition, bails out of the loop that it is currently inside. If there was any confusion as to where the last call would be tossing program flow to, a comment might help. The loop label indicates the start of the loop, not the finish, so you have to read backwards to find the label, then scan fowards to find the loop finish point.

      Why not this?
      while (something()) { # Lots of stuff, nested functions, etc. last if ($condition); # Move to Post-Processing # Lots more stuff } # Post-Processing more_stuff();
      Instead of:
      PROCESS: while (something()) { # Lots of stuff, nested functions, etc. last PROCESS if ($condition); # Lots more stuff } more_stuff();
      If the while() structure was very long, it might be easier to scan for a carefully worded comment.

      If you have a reason for putting loop labels in, by all means put them in. All I'm advocating is that there shouldn't be things in a program that are there for no reason.

      Or maybe I just don't like Perl programs which feel like:
      _10: print "Hello"; _20: goto _10;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found