Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Match string in array

by gasho (Beadle)
on Oct 03, 2005 at 19:09 UTC ( #497023=note: print w/replies, xml ) Need Help??

in reply to Match string in array

@FirstArray=qw(A B C D); @SecondArray=qw(D E F B); foreach $pr (@FirstArray){ #print "Property: $pr\n"; @match=(); @match=grep(/$pr/,@SecondArray); if (!@match){ push(@UniqueInFirstArrayNotFoundInSecondArray,$pr); } else { push(@FoundInBothArrays,$pr); } } print "".join("\n",@UniqueInFirstArrayNotFoundInSecondArray)."\n"; print "".join("\n",@FoundInBothArrays)."\n";

Replies are listed 'Best First'.
Re^2: Match string in array
by sauoq (Abbot) on Oct 03, 2005 at 19:18 UTC

    Everytime you use grep, you are iterating through the list. Since you do that inside of loop, your algorithm is O(N*M). In contrast, JediWizard iterates once through a loop so his algorithm is linear. His approach is also conceptually simpler which results in cleaner code.

    "My two cents aren't worth a dime.";

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-03-22 03:06 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.