kaushik9918 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys I have two arrays with the following elements inside them.
array#1 : sdjd dsjd ldlsd epw alask* djsdj array#2 : djsdj ldlsd epw alask<1> ghg alask<2>
I need to check if the elements inside both the arrays are present in each of the arrays. That is, I should be able to print out the exclusive elements in both the arrays. My problem is , I am getting stuck at elements which have * in the array#1. for example in the case above, alask* should match to alask<1> and alask<2> . How do I do this with a short code? any ideas? For the above two arrays, my result expected should be a print:
sdjd dsjd in array#1 not present in array#2 ghg in array#2 not present in array#1 alask* in #array1 matched to alask<1> alask<2> in #array2
thanks

Replies are listed 'Best First'.
Re: Compare two arrays
by Discipulus (Canon) on Dec 05, 2014 at 08:42 UTC
    hello this is a Categorized question with a lot of good modules to use. In addition you can also use the function is_deeply from the core module Test::More.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks everyone. I could manage to do it by splitting the input elements and using $1, $2 and the generic $_.
      if ($word =~ m/(.*)\<(.*)\>/){ chomp; push @array,$1; } else { push @array,$word; }
      I then proceed and compare the array elements once this formatting is done.
Re: Compare two arrays
by hdb (Monsignor) on Dec 05, 2014 at 10:12 UTC

    You can escape metacharacters in regular expressions using \Q and \E. Have a look at quotemeta.

      Thanks - the quotemeta function is very useful.
Re: Compare two arrays
by hippo (Archbishop) on Dec 05, 2014 at 09:12 UTC

    This is a FAQ: How do I compute the difference of two arrays? How do I compute the intersection of two arrays?. Do please read the FAQs. If you don't understand the answers there or have further questions arising from them not covered in the FAQs then by all means post here for more discussion.

    Update: As per Laurent_R's reply and the OP's node above the task appears not to be to compare two arrays but rather to match an array of patterns to an array of strings which is indeed a different problem (even if related). Apologies for my pre-caffiene noise.

      It seems to me that the OP is not really about computing the difference (or intersection) between two arrays, but more about how to do the match when one of the arrays may have a generic "*" character in some of its elements to represent some other characters. So probably more a regex question. I won't go further into that, since the original poster has apparently found a solution.
Re: Compare two arrays
by Anonymous Monk on Dec 05, 2014 at 07:36 UTC
    Why don't you post perl code?
      if ( @found = grep { $_ eq $element } @array ) { my $found = join ",", @found; print "Element exists\n"; }
      This is the code for elements found. But it doesn not work for equating elements "xyz*" with "xyz<1>" , "<xyz<2>" etc