Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re: Flag variables by KPeter0314
in thread Flag variables by oakbox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2014-11-22 03:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (118 votes), past polls