Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Compare two arrays

by Marklitz (Initiate)
on Apr 02, 2009 at 10:32 UTC ( #754906=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
I have to compare two string arrays. I have to check whether array1's element belong to array2 or not.
I have strings mixture of alphabets and numbers like:
array1 = {this1 this2 this11 this21 this23}; array2 = {h2this1 h3this2 h5this21 h6this23 h8this13}
i tried by
for $i(0.. $#array1){ for $j(0.. $#array2){ if($array2[$j]=~ /$array1[$i]/){ print"$array1[$j] belongs to $array2[$i]"; last; }}}
Unfortunately, it prints as:
this1 belongs to h2this1 this2 belongs to h3this2 this2 belongs to h5this21 this2 belongs to h6this23 this1 belongs to h8this13
instead of printing it as:
this1 belongs to h2this1 this2 belongs to h3this2 this21 belongs to h5this21 this23 belongs to h6this23 this1 belongs to h8this13
I seek your advice.
Thanking you,

Replies are listed 'Best First'.
Re: Compare two arrays
by ELISHEVA (Prior) on Apr 02, 2009 at 10:47 UTC
    You are matching "this2" to "h6this23" because your regex is missing a final dollar sign. Without the final $, matches in the middle of the string will print out. With it, only matches at the end of the string will print out. See regextut for more information on the $.

    So this should work: /$array1[$j]$/

    Best,beth

Re: Compare two arrays
by almut (Canon) on Apr 02, 2009 at 10:52 UTC

    ...also, you've swapped $i and $j, i.e. for $i(0.. $#array1), but then $array2[$i]  (doesn't really matter in this particular case, but it would, if the arrays were of different size).

    It's not entirely clear what you want to achieve. The "this1 belongs to h8this13" in your expected output looks like you would want to do substring matching, but then, the match "this2 belongs to h5this21", which you consider unfortunate, suggests you might not...

Re: Compare two arrays
by johngg (Canon) on Apr 02, 2009 at 11:44 UTC

    I may be missing something but if the last line of your desired output is this1 belongs to h8this13 then it seems logical that you would also have this2 belongs to h5this21 and this2 belongs to h6this23. Anchoring the match to the end of the string drops your last desired line. Perhaps you could clarify your requirement.

    use strict; use warnings; my @arr1 = qw{ this1 this2 this11 this21 this23 }; my @arr2 = qw{ h2this1 h3this2 h5this21 h6this23 h8this13 }; my @arr1Rx = map { [ $_, qr{$_\z} ] } @arr1; foreach my $e2 ( @arr2 ) { foreach my $e1 ( @arr1Rx ) { print qq{$e1->[ 0 ] belongs to $e2\n} if $e2 =~ $e1->[ 1 ]; } }

    The output

    this1 belongs to h2this1 this2 belongs to h3this2 this21 belongs to h5this21 this23 belongs to h6this23

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Compare two arrays
by targetsmart (Curate) on Apr 02, 2009 at 11:56 UTC
Re: Compare two arrays
by Bloodnok (Vicar) on Apr 02, 2009 at 11:26 UTC
    Why not utilise the hash based approach outlined in Recipe 4.8 of the Perl Cookbook ??

    A user level that continues to overstate my experience :-))
      ----A user level that continues to overstate my experience

      I guess that is why many of us are hanging here. :-))

      --Artist
Re: Compare two arrays
by leslie (Pilgrim) on Apr 02, 2009 at 11:37 UTC

    Make your question clear..

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2022-06-28 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (92 votes). Check out past polls.

    Notices?