Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Array compare

by plink (Initiate)
on Dec 22, 2007 at 11:46 UTC ( [id://658641]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I got two arrays, containing each one, four random digits ( from 0 to 5).
I would like to compare both of them in order to have:

1.)How many digits are in the same positions ( same index )
2.)How many digits are in different positions.

Ex.
Array_one 2 1 3 5
Array_two 2 3 1 0

One element ( 2 ) is in the same position ( index 0 ).
Two elements ( 3,1 ) in different position.

The first one, I can solve with a for/foreach loop

$match = 0;
for $i = 0; i < 4; $i++
$match++ if $array_one[$i] == $array_two[$i]

while the second one.....

Thanks
Claudio

Replies are listed 'Best First'.
Re: Array compare
by FunkyMonk (Chancellor) on Dec 22, 2007 at 12:05 UTC
    I'd use grep to find the exact matches and the numbers in one of the arrays (I used @A below) as keys in a hash (%in_A), built using a map. Finally another grep to find the matches in different columns:
    use List::Util 'shuffle'; my @A = (shuffle 0 .. 5)[0 .. 3]; my @B = (shuffle 0 .. 5)[0 .. 3]; my $exact = grep { $A[$_] == $B[$_] } 0 .. 3; my %in_A = map { $_ => 1 } @A; my $inexact = (grep { $in_A{$_} } @B) - $exact; print "A = [@A]\nB = [@B]\n"; print "exact: $exact\n"; print "inexact: $inexact\n";

    The inexact matches also includes the exact matches, hence the subtraction in the second grep.

    Sample output:

    A = [4 5 1 0] B = [3 1 5 0] exact: 1 inexact: 2

    update: added sample output, improved output format & used grep to calculate inexact matches

      Thank you all for help and solutions

      The code written by FunkyMonk is simple and straight.
      and it works...
Re: Array compare
by almut (Canon) on Dec 22, 2007 at 12:37 UTC

    Here's another approach:

    my @array_one = (2, 1, 3, 5); my @array_two = (2, 3, 1, 0); my %indices; for my $i (0..3) { # store at which index the elements occurred push @{$indices{$array_one[$i]}}, $i; push @{$indices{$array_two[$i]}}, $i; } my ($same_pos, $diff_pos) = (0, 0); for my $val (sort keys %indices) { my $i = $indices{$val}; if (@$i >= 2) { if ($i->[0] == $i->[1]) { $same_pos++; print "element '$val' in same position\n"; } else { $diff_pos++; print "element '$val' in different position\n"; } } } printf "=> %d element%s in same position, %d element%s in different po +sition\n", $same_pos, $same_pos==1 ? "":"s", $diff_pos, $diff_pos==1 ? "":"s";

    Output:

    element '1' in different position element '2' in same position element '3' in different position => 1 element in same position, 2 elements in different position

    Note that this assumes that an element occurs in either the same or in a different position, but not both (and that there's no more than one occurrence of each element within one array). I.e. something like this

    3, 1, 3, 5 3, 3, 1, 0

    will produce ambiguous output (report the first occurrence, that is).

Re: Array compare
by artist (Parson) on Dec 22, 2007 at 16:29 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

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

    No recent polls found