http://qs321.pair.com?node_id=240056


in reply to Flag variables

Not to be a troublesome monk, but I have a twist on the Wisdom that oakbox seeks. I too being just a learning monk with nobody local to learn from.

Given that the code in the original post is not what we should aspire to, what about looking for multiple matching items in the array? Say for instance that the array is not unique and you want to look for an instance of "foo" and also "bar". Either of these may show up once, many times, or even never. In any case we only wan to run the resulting action once

Would the code from blakem be the best? I hadn't used the grep fuction in this way and am glad to be illumined to it. It would seem to be expensive though if @somearray was rather large to iterate through it multiple times.

fooaction() if grep { $_ eq "foo" } @somearray; baraction() if grep { $_ eq "bar" } @somearray;
Or, would there be a better way than expanding the code to this if the @somearray were large:
my $fooflag = 0; my $barflag = 0; foreach my $var (@somearray){ if($var eq "foo"){ $fooflag = 1;} if($var eq "bar"){ $barflag = 1;} } if($fooflag eq "1"){ &fooaction;} if($barflag eq "1"){ &baraction;}
Note: you wouldn't want to jump out with a "last" as that would prevent you from checking for other conditions.

-Kurt