Array-Unique
view release on metacpan or search on metacpan
either remove duplicates on each update by that keeping the array
always unique or remove duplicates just before you want to use the
uniqueness feature of the array. In either case you might run a
function you call @a = unique_value(@a);
The problem with this approach is that you have to implement the
unique_value function (see later) AND you have to make sure you don't
forget to call it. I would say don't rely on remembering this.
There is good discussion about it in the 1st edition of the Perl
Cookbook of O'Reilly. I have copied the solutions here, you can see
further discussion in the book.
Extracting Unique Elements from a List (Section 4.6 in the Perl
Cookbook 1st ed.)
# Straightforward
%seen = ();
@uniq = ();
foreach $item (@list) [
unless ($seen{$item}) {
# if we get here we have not seen it before
$seen{$item} = 1;
push (@uniq, $item);
lib/Array/Unique.pm view on Meta::CPAN
our $VERSION = '0.09';
# Strips out any duplicate values (leaves the first occurrence
# of every duplicated value and drops the later occurrences).
# Removes all undef values.
sub unique {
my $self = shift; # self or class
my %seen;
my @unique = grep defined $_ && !$seen{$_}++, @_;
# based on the Cookbook 1st edition and on suggestion by Jeff 'japhy' Pinyan
# fixed by Werner Weichselberger
}
sub TIEARRAY {
my $class = shift;
my $self = {
array => [],
hash => {},
};
lib/Array/Unique.pm view on Meta::CPAN
always unique or remove duplicates just before you want to use the
uniqueness feature of the array. In either case you might run a
function you call @a = unique_value(@a);
The problem with this approach is that you have to implement
the unique_value function (see later) AND you have to make sure you
don't forget to call it. I would say don't rely on remembering this.
There is good discussion about it in the 1st edition of the
Perl Cookbook of O'Reilly. I have copied the solutions here,
you can see further discussion in the book.
Extracting Unique Elements from a List (Section 4.6 in the Perl Cookbook 1st ed.)
# Straightforward
%seen = ();
@uniq = ();
foreach $item (@list) [
unless ($seen{$item}) {
# if we get here we have not seen it before
$seen{$item} = 1;
push (@uniq, $item);
( run in 0.449 second using v1.01-cache-2.11-cpan-e9199f4ba4c )