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

Re: A way to avoid repeated conditional loops

by tinita (Parson)
on Aug 24, 2011 at 11:18 UTC ( #922082=note: print w/ replies, xml ) Need Help??


in reply to A way to avoid repeated conditional loops

use the flip-flop operator.
if you want to read from alpha until end of file:

while (<$fh>) { if ( (m/^alpha$/ .. eof($fh) ) > 1) { # everything between alpha and end of file } }
you can even change that second condition to 0: if ( (m/^alpha$/ .. 0 ) > 1)


Comment on Re: A way to avoid repeated conditional loops
Select or Download Code
Re^2: A way to avoid repeated conditional loops
by Deus Ex (Beadle) on Aug 24, 2011 at 12:44 UTC

    That's an awesome approach! Really like this method. I'll test it and put it on production.

    Thanks!

      Are you assuming that 'alpha' is also always the first line in the file?

      Also, the posted flipflop doesn't behave the same as the OP

      A simpler way to just use a flag and short circuit the regex work:

      my $seen = 0; while (<$fh>) { if ( !$seen and m/^alpha$/) { $seen = 'yes indeed'; print "true\n"; }else{ print "false\n"; } }

      Given a test file of:

      foo bar alpha beta gamma delta
      The results I got are:
      Original: false false true false false false Flipflop: Argument "" isn't numeric in numeric gt (>) at test.pl line 21, <$fh> +line 1. false Argument "" isn't numeric in numeric gt (>) at test.pl line 21, <$fh> +line 2. false false true true true Flag: false false true false false false

        No, "alpha" might be anywhere in the middle of the file (but it will appear only once).

        Actually the flip-flop operator may work only after "alpha" matched.

        Many thanks though for the short-circuiting thing :)

        hm, if I add no warnings "numeric"; to the flip-flop code, the output seems to be fine. output all lines after "alpha". at least that's how I understood the task.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-08-22 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls