Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: A way to avoid repeated conditional loops

by SuicideJunkie (Vicar)
on Aug 24, 2011 at 13:57 UTC ( [id://922117]=note: print w/replies, xml ) Need Help??


in reply to Re^2: A way to avoid repeated conditional loops
in thread A way to avoid repeated conditional loops

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

Replies are listed 'Best First'.
Re^4: A way to avoid repeated conditional loops
by tinita (Parson) on Aug 24, 2011 at 15:10 UTC
    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.

      The OP code has an else condition on the match for alpha. That should execute for every line of the file except the 'alpha', regardless of before or after.

Re^4: A way to avoid repeated conditional loops
by Deus Ex (Scribe) on Aug 24, 2011 at 14:43 UTC

    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 :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2025-11-07 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (65 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.