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

Comparing arrays in perl.

by anand_perl (Novice)
on Aug 06, 2008 at 05:31 UTC ( [id://702557]=perlquestion: print w/replies, xml ) Need Help??

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


Hi I want to compare two arrays and delete the occurances of elements in a array.

For example, let the two array's be:

@arr1 = ("one","two","three","four","five");

@arr2 = ("one","a","b","one","c","d","e","two","f","g","h","two","i","j","two");

I want to compare these two arrays.In arr2,there are elements of arr1 and these elements occur more than once in arr2.

For example,in arr2 element "one" is repeated twice, I want to delete the other occurance of the element "one".

Similarly in arr2 elelment "two" is repeated thrice, I want to delete the second and further occurance of this element "two".

Can anyone help me on this.....

Thanks in advance...

Anand.

Replies are listed 'Best First'.
Re: Comparing arrays in perl.
by ikegami (Patriarch) on Aug 06, 2008 at 07:01 UTC

    The standard way to get rid of duplicates is:

    my %seen; my @filtered = grep !$seen{$_}++, @arr2;

    The difference is that you only want to remove duplicates of elements present in @arr1. Let's adapt the above:

    my %seen; my %in_arr1 = map { $_ => 1 } @arr1; my @filtered = grep !$in_arr1{$_} || !$seen{$_}++, @arr2;
Re: Comparing arrays in perl.
by gaal (Parson) on Aug 06, 2008 at 05:55 UTC
    Your specification isn't very exact. Looks like you want two different functions. I suggest you take advantage of Perl's testing modules to specify what you want with several examples, and then play with code until all your tests pass. You can take advantage of eq_array to compare arrays. (Note: if your arrays contain references, you have more decisions to make.)

    For example one of the things you're looking for seems to be to delete duplicates while preserving order. Let's call this first_filter.

    use Test::More qw(no_plan); eq_array([first_filter(qw/one two three/)], [qw/one two three/], "clean list doesn't get changed"); eq_array([first_filter(qw/one one one/)], [qw/one/], "simple repetition removed"); eq_array([first_filter(qw/one two one/)], [qw/one two/], "interleaved repetition removed");

    ...and so on. The nice thing about this is that if I got it wrong, and this isn't the function you want, you can change the test and add more examples to clarify what you mean. When people offer you suggestions for implementation, you can with one line check their stuff and immediately see if it does everything you want it to.

Re: Comparing arrays in perl.
by prasadbabu (Prior) on Aug 06, 2008 at 05:43 UTC

    Hi anand_perl,

    Here is one way to do it. Also you can use some array modules to accomplish the same.

    use strict; use warnings; my @arr1 = ("one","two","three","four","five"); my @arr2 = ("one","a","b","one","c","d","e","two","f","g","h","two","i +","j","two"); my @arr3; my %hash; for my $arr2 (@arr2){ if (grep $arr2, @arr1){ $hash{$arr2}++; }else{ push (@arr3, $arr2); } } push (@arr3, keys %hash); print "@arr3"; output: ------- e a d two j c h one g b f i

    Prasad

Re: Comparing arrays in perl.
by Anonymous Monk on Aug 06, 2008 at 07:05 UTC
    I, also, think the specification is not exact, but I take it to be "uniqify all elements of @arr2 that also exist in @arr1". If that's the case, something like this might work:
    perl -wMstrict -le "my @arr1 = qw(one two three four five); my @arr2 = qw(one a b one c d e two f g h two i j two); my %seen1 = map { $_ => 0 } @arr1; @arr2 = grep { ! exists $seen1{$_} or ! $seen1{$_}++ } @arr2; print qq(@arr2); " one a b c d e two f g h i j
Re: Comparing arrays in perl.
by whereiskurt (Friar) on Aug 06, 2008 at 11:10 UTC

    Ananad,

    This may be 'too much information', but I was literally putting this example together for a colleague yesterday and thought it was applicable:

    use strict; use warnings; use Quantum::Superpositions; my @a = (1, 3, 5, 7, 9, 11); my @b = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); my @intersect = eigenstates(any(@a) == any(@b)); my @union = eigenstates(any(@a, @b)); my @diff = eigenstates(any(@b) != all(@a)); print join(', ', @intersect)."\n"; print join(', ', @union) ."\n"; print join(', ', @diff) ."\n";

    Which outputs:

    11, 1, 3, 7, 9, 5 6, 11, 3, 7, 9, 12, 2, 8, 1, 4, 10, 5 6, 12, 2, 8, 4, 10

    I guess the 'issue' is you need the 'Quantum::Superpositions' module (thanks Damian!), from what I hear it isn't super quick on larger arrays, and the output order isn't guaranteed.

    Perl6 may have operators that act very similar called junctions (any,all,none).

    Regards,
    Kurt

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found