Leser: 32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my @are_equal = compare_arrays(\@a, \@b);
sub compare_arrays {
my ($first, $second) = @_;
my @tmp;
no warnings; # silence spurious -w undef complaints
#return 0 unless @$first == @$second;
for (my $i = 0; $i < @$first; $i++)
{
if ($first->[$i] eq $second->[$i])
{
push(@tmp,$first->[$i]);
}
}
return @tmp;
}
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
#!/usr/bin/perl use strict; use warnings; my @a = qw(1 2 3 4 5 6); my @b = qw(9 2 7 6 5 4); my @are_equal = compare_arrays(\@a, \@b); print $_,"\n" for @are_equal; sub compare_arrays { my ($first, $second) = @_; my @tmp; no warnings; # silence spurious -w undef complaints #return 0 unless @$first == @$second; for (my $i = 0; $i < @$first; $i++) { if ($first->[$i] eq $second->[$i]) { push(@tmp,$first->[$i]); } } return @tmp; }
1 2
my @a = qw/ foo bar baz boo far rab oob /; my @b = qw/ foo BAR baz BOO far /;
1 2 3 4 5 6 7 8 9 10 11 12 13
sub compare_arrays { my ($first, $second) = @_; my @tmp; for my $elm (@$first) { if(defined($elm) && grep{defined($_) && $elm eq $_}@$second) { push(@tmp,$elm); } } return @tmp; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
sub compare_arrays { my ($first, $second) = @_; my %found; my @tmp; for my $elm (@$first) { if(defined($elm) && grep{defined($_) && $elm eq $_}@$second && !$found{$elm}) { push(@tmp,$elm); $found{$elm}++; } } return @tmp; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
sub compare_arrays { my ($first, $second) = @_; my %found=map{($_,1)}@$first; my @tmp; for my $elm (@$second) { if(defined($elm) && $found{$elm}) { push(@tmp,$elm); } } return @tmp; }