in reply to Removing Duplicates from Array Passed by Reference
sub util_remove_duplicates { my $ref = ref($_[0]) ? shift() : [@_]; my %hash; for (my $i = 0; $i <= $#$ref; $i ++) { if (! $hash{$ref->[$i]}) { $hash{$ref->[$i]} = 1; next; } splice @$ref, $i, 1; $i --; } return wantarray ? @$ref : $ref; } my @a = qw(a a b c d a d c a e); util_remove_duplicates(\@a); # works my @B = util_remove_duplicates(\@a); # works and returns array to @B my $B = util_remove_duplicates(\@a); # works and returns array ref to +$B my $B = util_remove_duplicates(@a); # works without modifying @a
|
|---|