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

Re: Check Array Elements

by NetWallah (Canon)
on Jun 06, 2012 at 22:25 UTC ( [id://974847]=note: print w/replies, xml ) Need Help??


in reply to Check Array Elements

Look Ma ! - No temp hash! (Good if you are doing this check only a small number of times).
if ((my $x=grep {/ABC|GHI|MNO/}@FILES) == 3){ print qq|Found them all\n| }else{ print qq|found only $x\n| }
Update Ignore the buggy version above. Revised, based on morgons (++) observations below ..
my @need=sort qw|ABC GHI MNO|; my $flat=join "",@need; my $re="\\b" . join( "|",@need)."\\b"; if ($flat eq join( "", sort grep{m/$re/}@FILES)){ say "Found!" }

             I hope life isn't a big joke, because I don't get it.
                   -SNL

Replies are listed 'Best First'.
Re^2: Check Array Elements
by morgon (Priest) on Jun 06, 2012 at 22:41 UTC
    This is wrong.

    Counterexample:

    @FILES = ("ABC", "ABC", "ABC");
    Your grep would still report 3, event though "GHI" is missing.

    And that is even without mentioning that the regex matches also enties like "XABCY".

      Thanks for pointing out the bugs (++). Code updated above.

      I think that method is getting unwieldy. Much prefer ckjs approach below.

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

        serious ++. I love seeing this type of accountability. I know I often have to swallow it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found