NAME
Array::Unique - Arrays that allow only unique values
SYNOPSIS
use Array::Unique;
tie @a, Array::Unique;
Use @a as a regular arrray.
DESCRIPTION
There is not much to say here.
The module provides you with a tie-able array
that allows only unique values.
It does not allow undef as a value in the array.
DISCUSSION
When you are collecting a list of items and you want
to make sure there is only one occurence of each item,
you have several option:
- 1) using hash
-
Some people use the keys of a hash to keep the items and put an arbitrary value as the values of the hash: To build such a list: %unique = map { $_ => 1 } qw( one two one two three four! ); To print it: print join ", ", sort keys %unique; To add values to it: %unique = map { $_ => 1 } (keys %unique, qw( one after the nine oh nine )); To remove values: delete @unique{ qw(oh nine) }; To check if a value is there: $unique{ $value }; # which is why I like to use "1" as my value (thanks to Gaal Yahas for the above explanation) There are three drawbacks I see: 1) You type more. 2) Your reader might not understand at first why did you use hash and what will be the values. 3) You lose the order. Usually non of them is critical but when I saw this the 10th time in a code I had to understand with 0 documentation I got frustrated.
- 2) using array
-
Other people might use real arrays and on each update or before they want to use the uniqueness feature of the array they might run a function they call @a = unique_value(@); This is also good but you have to implement the unique_value function AND you have to make sure you don't forget to call it. Something I have a tendency to do just before code release.
- 3) using Array::Unique
-
So I decided to write this module because I got frustrated by my lack of understanding what's going on in that code I mentioned. In addition I thought it can a good game to write this and then benchmark it. Additionally it is nice to have your name displayed in bright lights all over CPAN ... or at least in a module. Array::Unique lets you tie an array to hmmm, itself (?) and makes sure the values of the array are always unique.
TODO
Benchmark speed
Add faster functions that don't check uniqueness so if I
know part of the data that comes from a uniques source then
I can speed up the process,
In short shoot myself in the leg.
Enable optional compare with other functions
Setup other implementations
AUTHOR
Gabor Szabo <gabor@perl.org.il>
COPYRIGHT AND LICENCE
Copyright (C) 2002 Gabor Szabo <gabor@perl.org.il>
All rights reserved.
You may distribute under the terms of either the GNU
General Public License or the Artistic License, as
specified in the Perl README file.
No WARRANTY whatsoever.