[Milan-pm] comparing 2 lists

marcos rebelo oleber at gmail.com
Tue Oct 6 03:49:56 PDT 2009


On Tue, Oct 6, 2009 at 12:44 PM, marcos rebelo <oleber at gmail.com> wrote:
> On Tue, Oct 6, 2009 at 12:38 PM, Paola Bisignano
> <paola.bisignano at gmail.com> wrote:
>> yes I mean that they are not in the same order and I only need to
>> return the elements that are both in the two array
>>
>> 2009/10/6 Lally Blue <lally22 at gmail.com>:
>>> are the 2 arrays items in the same order?
>>> in the reference it says:
>>> "Internally the comparator compares the two arrays by using join to turn
>>> both arrays into strings and comparing the strings using eq."
>>> so I guess that if the first array has 1,2,3,4,5 and the second one
>>> 5,4,3,2,1 the Array::Compare returns false...
>>> is this what you mean?
>>>   -Ilaria-
>>> On Tue, Oct 6, 2009 at 12:32 PM, Paola Bisignano <paola.bisignano at gmail.com>
>>> wrote:
>>>>
>>>> Hi,
>>>> I have two lists of numbers, saved in two files and I want to compare
>>>> them, in order to check which are the same elements in both lists. I
>>>> put them into array, so I want to compare array, but I only want to
>>>> check the value, and It doesn't matter for the position in the array,
>>>> I tried to use Array::Compare
>>>>
>>>>
>>>>
>>>> open (OUT, "> $file");
>>>>                                        my $comp = Array::Compare-> new;
>>>>                                        if ($comp->compare(\@array_num1,
>>>> \@array_num2))
>>>>                                                {
>>>>                                                print "$comp\n";
>>>>                                                print OUT "$comp\n";
>>>>                                                }
>>>>                                        close OUT;
>>>>
>>>> but it returns blank, I don't know why...
>>>> _______________________________________________
>>>> Milan-pm mailing list
>>>> Milan-pm at pm.org
>>>> http://mail.pm.org/mailman/listinfo/milan-pm
>>>
>>>
>>> _______________________________________________
>>> Milan-pm mailing list
>>> Milan-pm at pm.org
>>> http://mail.pm.org/mailman/listinfo/milan-pm
>>>
>>>
>> _______________________________________________
>> Milan-pm mailing list
>> Milan-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/milan-pm
>>
>
> something like:
>
>
> use strict;
> use warnings;
>
> my %hash;
> open(my $FH_1, '<', '/file/path_1');
> @hash{readline($FH_1)} = ();
> close $FH_1;
>
> open(my $FH_2, '<', '/file/path_2');
> while (my $line = readline($FH_2)) {
>    if ( exists $hash{$line} ) {
>        print $line;
>    }
> }
> close $FH_2;
>
>
>
> it will compare the numbers as strings, and print the repetitions.
>
> Best Regards
> Marcos Rebelo
>
> --
> Marcos Rebelo
> http://oleber.freehostia.com
> Milan Perl Mongers leader http://milan.pm.org
>

similar code using your arrays:

my %hash;
@hash{@array_num1} = ();

open (my $OUT, '>', $file);

foreach my $number (@array_num2) {
    if ( exists $hash{$number} ) {
        print "$number\n";
        print $OUT "$number\n";
    }
}

close $OUT;


Best Regards
Marcos Rebelo


-- 
Marcos Rebelo
http://oleber.freehostia.com
Milan Perl Mongers leader http://milan.pm.org


More information about the Milan-pm mailing list