Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2021-04-21 14:45 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found