|< 1 2 3 >| | 24 Einträge, 3 Seiten |
1
2
3
4
5
6
use strict;
my @ar1=qw/hallo du wie geht das/;
my @ar2=qw/hallo du/;
my %ar2; @ar2{@ar2}=();
my @ar3 = grep {! exists $ar2{$_}} @ar1;
print "$_\n" for @ar3;
1
2
3
4
5
6
7
use strict;
my @ar1=qw/1 2 2 3 3 3 4 4 4 4/;
my @ar2=qw/1 2 3 4/;
my %ar2;
$ar2{$_} ++ for @ar2;
my @ar3 = grep {not($ar2{$_} and $ar2{$_}--)} @ar1;
print "$_\n" for @ar3;
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use strict;
use warnings;
use diagnostics;
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 "Ergebnis a:\n", Dumper \@a;
my @b1 = qw/eins eins zwei/;
my @b2 = qw/eins zwei/;
my @b = array_differenz(\@b1, \@b2);
print "Ergebnis b:\n", Dumper \@b;
}
sub array_differenz ($$) {
my $a1 = shift; # Array-Referenz
my $a2 = shift; # Array-Referenz
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);
}
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 = [];
Ergebnis a:
$VAR1 = [
'2',
'3',
'3',
'4',
'4',
'4'
];
Array 1:
$VAR1 = [
'eins',
'eins',
'zwei'
];
Array 2:
$VAR1 = [
'eins',
'zwei'
];
Array 1 minus Array 2:
$VAR1 = [
'eins'
];
Array 2 minus Array 1:
$VAR1 = [];
Ergebnis b:
$VAR1 = [
'eins'
];
Quote\n\nDie Differenz zweier Arrays A und B ist gleich der Vereinigung von A ohne B mit B ohne A.
|< 1 2 3 >| | 24 Einträge, 3 Seiten |