|1) using an array and extracting the unique elements later||
You might use a regular array to hold this unique set of values
and 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 dont forget to call it. I would say dont rely on remembering this.
There is good discussion about it in the 1st edition of the Perl Cookbook of OReilly. 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.)
# Faster but different
|2) using a 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:
To print it:
To add values to it:
To remove values:
To check if a value is there:
(thanks to Gaal Yahas for the above examples)
There are three drawbacks I see:
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.
|3) using Array::Unique||
So I decided to write this module because I got frustrated
by my lack of understanding whats going on in that code
In addition I thought it might be interesting 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.
|4) Using real SET||There are modules on CPAN that let you create and maintain SETs. I have not checked any of those but I guess they just as much of an overkill for this functionality as Unique::Array.|
use Array::Unique; tie @a, Array::Unique; @c = @a = qw(a b c a d e f b); @c will contain the same as @a AND two undefs at the end because @c you get the same length as the right most list.
Change size of the array Elements with false values (, 0, 0)
splice: splice @a; splice @a, 3; splice @a, -3; splice @a, 3, 5; splice @a, 3, -5; splice @a, -3, 5; splice @a, -3, -5; splice @a, ?, ?, @b;
Add faster functions that dont check uniqueness so if I know part of the data that comes from a unique source then I can speed up the process, In short shoot myself in the leg.
Enable optional compare with other functions
Write even better implementations.
Gabor Szabo <email@example.com>
Copyright (C) 2002-2008 Gabor Szabo <firstname.lastname@example.org> All rights reserved. http://www.pti.co.il/
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.
Thanks for suggestions and bug reports to Szabo Balazs (dLux) Shlomo Yona Gaal Yahas Jeff japhy Pinyan Werner Weichselberger
Date: 2008 June 04
|perl v5.20.3||ARRAY::UNIQUE (3)||2008-06-04|