http://qs321.pair.com?node_id=1060047


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...