in reply to Re: Re: Interesting Use of Grep
in thread Interesting Use of Grep

not trying to re-obfuscate what we were trying to make simpler, but this would move everything back into one statement, instead of trying for $found and then testing it:

for (LIST) { do{ something; last; } if ($_ eq $wanted) }

Replies are listed 'Best First'.
Re: Re: Re: Re: Interesting Use of Grep
by jynx (Priest) on Jun 29, 2001 at 01:04 UTC

    just a point here,

    i've been burned by this one many times, please don't try to use a loop control statement (ie next, last, redo) inside of a do block. it'll fail1. And from personal experience, whenever you try to think of doing something that involves a loop control statement inside a do block, stop yourself from implementing said code. Your best bet would be to rethink what you're doing, and failing that ask the monks here for help. It is possible (and likely) that it is the best code for some situations, but more oft than not it won't be what you're looking for after you've written it.

    i'll also make the point, you could do a rewrite kinda like so:

    for(LIST) { do{ something } and last if ($_ eq $wanted) }
    which would work, but it's not very readable and there are definitely better solutions.

    Hope This Helps,
    jynx

    1 For further support of this look up p112 in Camel3.

      That is odd, because this is a fairly common structure using do and last to emulate a switch statement. I use it all the time and have had no problems with it:

      for( $test_value ) { /pattern1/ and do { something; last; }; /pattern2/ and do { something else; last; }; do { default action; #no last required }; }

        Ok,

        Apparently you're right. Earlier this year i wrote perl5.6 code that did what you said and it didn't work. It specifically didn't work because the next/last tags inside the do block weren't working. Now i write a sample script to test and be certain that it doesn't work and it does. i'm very aware of the annonymous monk's posting below saying about false values not working for the and statement, that's obvious and the code i wrote didn't make such an error.

        Well, thank you, i learned something the hard way. i'll swear on my camel that the code didn't work, but now i know different. time to go back and recode i guess...

        thank you much,
        jynx

      And if something is something like $something=0 it will FAIL!