[Milan-pm] comparing 2 lists

marcos rebelo oleber at gmail.com
Tue Oct 6 09:09:13 PDT 2009


2009/10/6 Gianluca Casati <casati_gianluca at yahoo.it>:
>
> #Hi Paola,
> #this is the way I would do it, hope it can help.
>
> use strict;
>
> # le due liste, che tu leggerai dai file, qua le ho valorizzate a caso per
> fare delle prove.
> my @list1 = ( 1,2,3);
> my @list2 = (2,3,4);

try it with
my @list1 = ( 1, 1,2,3);
my @list2 = (2,3,4);

>
> # questo hash lo uso per contare quante volte mi si presenta un valore.
> my %count;
>
> # lista che conterrà i numeri in comune.
> my @common;
>
> # ciclo sulle due liste.
> for my $num ( @list1 , @list2 )
> {
> # ogni valore delle liste lo metto come chiave dell' hash e aggiungo 1, così
> conto quante volte ho trovato il numero.
> $count{$num}++;
> }
>
> for my $num ( keys %count )
> {
> # ciclo sulle chiavi dell' hash e metto i numeri che ho trovato più di una
> volta, nella lista @common.
> push( @common , $num ) if $count{$num} > 1;
> }
>
> # ti lascio la print, se vuoi stampare il risultato.
> #print "$_\n" for @common;
>
>
>
>
> ________________________________
> Da: Paola Bisignano <paola.bisignano at gmail.com>
> A: milan-pm at pm.org
> Inviato: Martedì 6 ottobre 2009, 12:32:08
> Oggetto: [Milan-pm] comparing 2 lists
>
> 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
>
>



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


More information about the Milan-pm mailing list