If you want to do something special with your arrays you could write your own function.
This one I wrote a while ago to solve a special problem, perhaps it is of use for you or give other hints:
`#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub array_differenz ($$);
main();
exit;
sub main {
my @a1 = qw/1 2 2 3 3 3 4 4 4 4/;
my @a2 = qw/1 2 3 4/;
my @a = array_differenz(\@a1, \@a2);
print "Result a:\n", Dumper \@a;
print "-"x40, "\n";
my @b1 = qw/one one two/;
my @b2 = qw/one two/;
my @b = array_differenz(\@b1, \@b2);
print "Result b:\n", Dumper \@b;
print "-"x40, "\n";
my @c1 = qw/one one two/;
my @c2 = qw/one two three/;
my @c = array_differenz(\@c1, \@c2);
print "Result c:\n", Dumper \@c;
} # sub main
sub array_differenz ($$) {
my $a1 = shift; # array reference
my $a2 = shift; # array reference
my @a1m2 = @$a1; # array 1 minus array 2;
for my $element (@$a2) {
for my $index (0..$#a1m2) {
if ($element eq $a1m2[$index]) {
splice @a1m2, $index, 1;
last;
}
}
}
my @a2m1 = @$a2; # array 2 minus array 1;
for my $element (@$a1) {
for my $index (0..$#a2m1) {
if ($element eq $a2m1[$index]) {
splice @a2m1, $index, 1;
last;
}
}
}
print "Array 1:\n", Dumper $a1;
print "Array 2:\n", Dumper $a2;
print "Array 1 minus Array 2:\n", Dumper \@a1m2;
print "Array 2 minus Array 1:\n", Dumper \@a2m1;
return (@a1m2, @a2m1);
} # sub array_differenz
`
it prints:
`Array 1:
$VAR1 = [
'1',
'2',
'2',
'3',
'3',
'3',
'4',
'4',
'4',
'4'
];
Array 2:
$VAR1 = [
'1',
'2',
'3',
'4'
];
Array 1 minus Array 2:
$VAR1 = [
'2',
'3',
'3',
'4',
'4',
'4'
];
Array 2 minus Array 1:
$VAR1 = [];
Result a:
$VAR1 = [
'2',
'3',
'3',
'4',
'4',
'4'
];
----------------------------------------
Array 1:
$VAR1 = [
'one',
'one',
'two'
];
Array 2:
$VAR1 = [
'one',
'two'
];
Array 1 minus Array 2:
$VAR1 = [
'one'
];
Array 2 minus Array 1:
$VAR1 = [];
Result b:
$VAR1 = [
'one'
];
----------------------------------------
Array 1:
$VAR1 = [
'one',
'one',
'two'
];
Array 2:
$VAR1 = [
'one',
'two',
'three'
];
Array 1 minus Array 2:
$VAR1 = [
'one'
];
Array 2 minus Array 1:
$VAR1 = [
'three'
];
Result c:
$VAR1 = [
'one',
'three'
];
`
HTH |
Comment onHow do I compare two arrays?