Revision 46e1506b
Von Moritz Bunkus vor mehr als 15 Jahren hinzugefügt
SL/MoreCommon.pm | ||
---|---|---|
3 | 3 |
require Exporter; |
4 | 4 |
@ISA = qw(Exporter); |
5 | 5 |
|
6 |
@EXPORT = qw(save_form restore_form compare_numbers any cross); |
|
6 |
@EXPORT = qw(save_form restore_form compare_numbers any cross); |
|
7 |
@EXPORT_OK = qw(ary_union ary_intersect ary_diff); |
|
7 | 8 |
|
8 | 9 |
use YAML; |
9 | 10 |
|
... | ... | |
127 | 128 |
} 0 .. $#A; |
128 | 129 |
} |
129 | 130 |
|
131 |
sub _ary_calc_union_intersect { |
|
132 |
my ($a, $b) = @_; |
|
133 |
|
|
134 |
my %count = (); |
|
135 |
|
|
136 |
foreach my $e (@$a, @$b) { $count{$e}++ } |
|
137 |
|
|
138 |
my @union = (); |
|
139 |
my @isect = (); |
|
140 |
foreach my $e (keys %count) { |
|
141 |
push @union, $e; |
|
142 |
push @isect, $e if $count{$e} == 2; |
|
143 |
} |
|
144 |
|
|
145 |
return (\@union, \@isect); |
|
146 |
} |
|
147 |
|
|
148 |
sub ary_union { |
|
149 |
return @{ (_ary_calc_union_intersect @_)[0] }; |
|
150 |
} |
|
151 |
|
|
152 |
sub ary_intersect { |
|
153 |
return @{ (_ary_calc_union_intersect @_)[1] }; |
|
154 |
} |
|
155 |
|
|
156 |
sub ary_diff { |
|
157 |
my ($a, $b) = @_; |
|
158 |
my %in_b = map { $_ => 1 } @$b; |
|
159 |
return grep { !$in_b{$_} } @$a; |
|
160 |
} |
|
161 |
|
|
130 | 162 |
1; |
Auch abrufbar als: Unified diff
Funktionen für Arrayberechnungen (Schnittmenge, Durchschnitt, Differenz).