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

Hello guys ! If i have 2 arrays;
@a=(0,1,0,1,0,0,0,1); @b=(0,0,1,1,0,0,0,0);
How do i compare those two arrays whether the value on position 0 in @a is equal with the value on position 0 in @b ? i tried with this but it does not work with :
@intersection = @difference = (); %count = (); foreach $element (@a, @b) { $count{$element}++ } foreach $element (keys %count) { push @{ $count{$element} > 1 ? \@intersection : \@difference }, $ele +ment; } foreach (@difference) { print $_."--"; }
because it is checking whether values of elements are equal.

Replies are listed 'Best First'.
Re: Compare two arrays
by davido (Cardinal) on Feb 01, 2012 at 08:32 UTC

    You want to compare each element of @a against the element of the same index in @b, right? This will do it.

    print "Index: $_ => \@a: $a[$_], \@b: $b[$_]\n" for grep { $a[$_] != $b[$_] } 0 .. $#a;

    ...or...

    foreach( 0 .. $#a ) { print "Index: $_ => \@a: $a[$_], \@b: $b[$_]\n" if $a[$_] != $b[$_ +]; }

    Which you choose kind of depends on whether you're more interested in keeping a list of indices to the dissimilar elements, or simply interested in processing the mismatches one by one. The grep version is handy for getting the list of mismatches.


    Dave

Re: Compare two arrays
by Anonymous Monk on Feb 01, 2012 at 08:33 UTC
Re: Compare two arrays
by Anonymous Monk on Feb 01, 2012 at 08:27 UTC
    I think i found a way to do it
    my $count=0; for ( my $i=0; $i <($#a+1);$i++) { if ( $a[$i] != $b[$i] ) { $count++; } } print $count;
Re: Compare two arrays
by rustic (Monk) on Feb 01, 2012 at 13:22 UTC
    try use Algorithm::Diff

      Hi Guys! I am very new to perl I have similar kind of problem where I need to compare 2 files , lets say : file1 contents= M|_|03-04-2012_08:21:03|_|Device1|_|8|_|Int/0.101|_| M|_|03-04-2012_08:01:05|_|Device1|_|8|_|Int/0.101|_| C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router P|_|04-04-2012_12:37:14|_|Device4_|CPU|_|CPU| File2 contents are : C|_|09-03-2011_06:49:52|_|Device5|_|cpu-1|_|cpu-1| P|_|04-04-2012_12:37:14|_|Device4|_|CPU|_|CPU C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router I have some conditions which I need to chcek and then filter desired o/p , those conditions are as below :

      if(($array1[0]=~/c/i) && ($array2[0]= ~/c/i)) { if ($arra1[2]=array2[2] && array1[4]=array2[4]) { don't add anything } else { append matched line to file2 } } elsif(($array1[0]=~/M/i) && ($array2[0]= ~/M/i)) { if ($arra1[2]=array2[2] && array1[4]=array2[4]) { don't add anything } else { append matched line to file2 } }
      I tried to apply this logic in below code but not working as expected , please help !

      #!/usr/local/bin/perl use strict; my @array1; my @array2; #my $count=0; my $i=0; open (OUTFILE ,">file2.dat") || die "can't create" ; open (FILE1,"file1.dat"); while(my $value1=<FILE1>) { my $i= $i +1 ; @array1=split('\|_\|',$value1); chomp($value1); my $dev1=$array1[2] ; my $attribute1=$array1[4]; my $profile1=$array1[0]; open (FILE2, "file2.dat"); while (my $value2=<FILE2>) { @array2=split('\|_\|',$value2); chomp($value2); my $dev2=$array2[2] ; my $attribute2=$array2[4]; my $profile2=$array2[0]; if(($profile1 = ~/C/i) and ($profile2 = ~/C/i)) { if(($dev1 = $dev2) && ($attribute1 = $attribute2)) { $i ++ ; } else { print OUTFILE "$value1" ; } } else { if(($profile1 = ~/M/i) and ($profile2 = ~/M/i)) { if(($dev1 = $dev2) && ($attribute1 = $attribute2)) { $i ++ ; } else { print OUTFILE "$value1" ; } } } } } close FILE1; close FILE2; close OUTFILE; }