Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Matching 2 unordered arrays then printing the first array highlighting the matches?

by hdb (Monsignor)
on Oct 28, 2013 at 19:27 UTC ( #1060047=note: print w/replies, xml ) Need Help??


in reply to Matching 2 unordered arrays then printing the first array highlighting the matches?

You create a hash with all elements of array2 as the keys. When iterating through array1, you check whether the elements exists in the hash.

my %match; undef @match{ @array2 }; for (@array1) { print "$_",exists $match{$_}?" match":"","\n"; }
  • Comment on Re: Matching 2 unordered arrays then printing the first array highlighting the matches?
  • Download Code

Replies are listed 'Best First'.
Re^2: Matching 2 unordered arrays then printing the first array highlighting the matches?
by Kenosis (Priest) on Oct 28, 2013 at 19:41 UTC

    Or just use Perl's 'smart' operator (no hash needed):

    print $_ ~~ @array2 ? "$_: Match!\n" : "$_: No Match\n" for @array1;
Re^2: Matching 2 unordered arrays then printing the first array highlighting the matches?
by Kenosis (Priest) on Oct 28, 2013 at 19:28 UTC

    Much better...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2023-01-27 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?