Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: in_array and skipping foreach

by JediWizard (Deacon)
on Apr 13, 2005 at 14:34 UTC ( [id://447416]=note: print w/replies, xml ) Need Help??


in reply to in_array and skipping foreach

First of all: You can use loop labels to controll which loop a next or last pertains to:

OUTER: foreach my $item (@array){ foreach my $subitem (@array2){ next OUTER if($subitem eq $item); } }

But to actually answer your question: I think it would be better to use grep in a scalar context to test for the presence of an item in an array (as opposed to foreach'ing through the array doing an eq comparison).

foreach my $item (@array){ next if(scalar(grep({$_ eq $item} @other_array))); }

see grep, next and last

HTH. Cheers

Update: As others have suggested, a hash is better for this type of lookup


A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

—His Holiness, The Dalai Lama

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found