Leser: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/perl use strict; use warnings; my @arr1 = ( ['a', 1, 2, 3], ['b', 4, 4, 3], ['d', 5, 2, 'H'], ); my @arr2 = ( ['a', 1, 2, 3], ['c', 8, 'r', 9], ['d', 5, 'f', 'H'], ); for my $pos (0..$#arr1) { my $l1=join(':',@{$arr1[$pos]}); my $l2=join(':',@{$arr2[$pos]}); if($arr1[$pos][0] eq $arr2[$pos][0]) { if($l1 eq $l2) { print "POS: $pos in beiden ok ($l1)\n"; } else { print "POS: $pos in beiden nicht ok ($l1) <> ($l2)\n" } } else { print "POS: $pos: Nur in arr1 ($l1)\n"; print "POS: $pos: Nur in arr2 ($l2)\n"; } }
1 2 3 4 5 6 7 8 9 10
my @arr1 = ( ['a', 1, 2, 3], ['b', 4, 4, 3], ['d', 5, 2, 'H'], ); my @arr2 = ( ['a', 1, 2, 3], ['d', 5, 'f', 'H'], ['c', 8, 'r', 9], );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/perl use strict; use warnings; my %hash1 = ( 'a' => [1, 2, 3], 'b' => [4, 4, 3], 'd' => [5, 2, 'H'], ); my %hash2 = ( 'a' => [1, 2, 3], 'd' => [5, 'f', 'H'], 'c' => [8, 'r', 9], ); for my $schluessel (keys %hash1) { if ($hash2{$schluessel}) { if (join(",", @{$hash1{$schluessel}}) eq join(",", @{$hash2{$schluessel}})) { print "Schluessel $schluessel in beiden ok.\n"; } else { print "Schluessel $schluessel in beiden nicht ok.\n"; } } else { print "Schluessel $schluessel nur in hash1.\n"; } } for my $schluessel (keys %hash2) { unless ($hash1{$schluessel}) { print "Schluessel $schluessel nur in hash2.\n"; } }
if ("@{$hash1{$schluessel}}" eq "@{$hash2{$schluessel}}") { ... }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
use strict;
use warnings;
my %hash1 = (
'a' => [1, 2, 3],
'b' => [4, 4, 3],
'd' => [5, 2, 'H'],
);
my %hash2 = (
'a' => [1, 2, 3],
'd' => [5, 'f', 'H'],
'c' => [8, 'r', 9],
);
while (my ($k,$v1)= each %hash1){
if (my $v2 = $hash2{$k}) {
print "$k ungleich" if "@$v1" ne "@$v2" ;
}
}
1
2
3
4
5
6
while (my ($k,$v1)= each %hash1){
local $"="\0";
if (my $v2= $hash2{$k}) {
print "$k ungleich" if "@$v1" ne "@$v2" ;
}
}
$hash{x}=undef
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl use strict; use warnings; my @arr1 = ( ['a', 1, 2, 3], ['b', 4, 4, 3], ['d', 5, 2, 'H'], ); my @arr2 = ( ['a', 1, 2, 3], ['c', 8, 'r', 9], ['d', 5, 'f', 'H'], ); my %joined; push(@{$joined{$_->[0]}},{ref=>$_, name=>'array1'}) for(@arr1); push(@{$joined{$_->[0]}},{ref=>$_, name=>'array2'}) for(@arr2); for(values(%joined)) { if(@$_==1) { my $lst=join(', ',@{$_->[0]->{ref}}); print "($lst) nur in $_->[0]->{name}\n"; } else { my $lst1=join(', ',@{$_->[0]->{ref}}); my $lst2=join(', ',@{$_->[1]->{ref}}); if($lst1 eq $lst2) { print "In beiden ok ($lst1)\n"; } else { print "In beiden nicht ok ($lst1) <> ($lst2)\n"; } } }