1Array::Unique(3)      User Contributed Perl Documentation     Array::Unique(3)
2
3
4

NAME

6       Array::Unique - Tie-able array that allows only unique values
7

SYNOPSIS

9        use Array::Unique;
10        tie @a, 'Array::Unique';
11
12        Now use @a as a regular array.
13

DESCRIPTION

15       This package lets you create an array which will allow only one
16       occurrence of any value.
17
18       In other words no matter how many times you put in 42 it will keep only
19       the first occurrence and the rest will be dropped.
20
21       You use the module via tie and once you tied your array to this module
22       it will behave correctly.
23
24       Uniqueness is checked with the 'eq' operator so among other things it
25       is case sensitive.
26
27       As a side effect the module does not allow undef as a value in the
28       array.
29

EXAMPLES

31        use Array::Unique;
32        tie @a, 'Array::Unique';
33
34        @a = qw(a b c a d e f);
35        push @a, qw(x b z);
36        print "@a\n";          # a b c d e f x z
37

DISCUSSION

39       When you are collecting a list of items and you want to make sure there
40       is only one occurrence of each item, you have several option:
41
42       1) using an array and extracting the unique elements later
43           You might use a regular array to hold this unique set of values and
44           either remove duplicates on each update by that keeping the array
45           always unique or remove duplicates just before you want to use the
46           uniqueness feature of the array. In either case you might run a
47           function you call @a = unique_value(@a);
48
49           The problem with this approach is that you have to implement the
50           unique_value function (see later) AND you have to make sure you
51           don't forget to call it. I would say don't rely on remembering
52           this.
53
54           There is good discussion about it in the 1st edition of the Perl
55           Cookbook of O'Reilly. I have copied the solutions here, you can see
56           further discussion in the book.
57
58           Extracting Unique Elements from a List (Section 4.6 in the Perl
59           Cookbook 1st ed.)
60
61           # Straightforward
62
63            %seen = ();
64            @uniq = ();
65            foreach $item (@list) [
66                unless ($seen{$item}) {
67                  # if we get here we have not seen it before
68                  $seen{$item} = 1;
69                  push (@uniq, $item);
70               }
71            }
72
73           # Faster
74
75            %seen = ();
76            foreach $item (@list) {
77              push(@uniq, $item) unless $seen{$item}++;
78            }
79
80           # Faster but different
81
82            %seen;
83            foreach $item (@list) {
84              $seen{$item}++;
85            }
86            @uniq = keys %seen;
87
88            # Faster and even more different
89            %seen;
90            @uniq = grep {! $seen{$_}++} @list;
91
92       2) using a hash
93           Some people use the keys of a hash to keep the items and put an
94           arbitrary value as the values of the hash:
95
96           To build such a list:
97
98            %unique = map { $_ => 1 } qw( one two one two three four! );
99
100           To print it:
101
102            print join ", ", sort keys %unique;
103
104           To add values to it:
105
106            $unique{$_}=1 foreach qw( one after the nine oh nine );
107
108           To remove values:
109
110            delete @unique{ qw(oh nine) };
111
112           To check if a value is there:
113
114            $unique{ $value };        # which is why I like to use "1" as my value
115
116           (thanks to Gaal Yahas for the above examples)
117
118           There are three drawbacks I see:
119
120           1) You type more.
121           2) Your reader might not understand at first why did you use hash
122           and what will be the values.
123           3) You lose the order.
124
125           Usually non of them is critical but when I saw this the 10th time
126           in a code I had to understand with 0 documentation I got
127           frustrated.
128
129       3) using Array::Unique
130           So I decided to write this module because I got frustrated by my
131           lack of understanding what's going on in that code I mentioned.
132
133           In addition I thought it might be interesting to write this and
134           then benchmark it.
135
136           Additionally it is nice to have your name displayed in bright
137           lights all over CPAN ... or at least in a module.
138
139           Array::Unique lets you tie an array to hmmm, itself (?)  and makes
140           sure the values of the array are always unique.
141
142           Since writing this I am not sure if I really recommend its usage.
143           I would say stick with the hash version and document that the
144           variable is aggregating a unique list of values.
145
146       4) Using real SET
147           There are modules on CPAN that let you create and maintain SETs.  I
148           have not checked any of those but I guess they just as much of an
149           overkill for this functionality as Unique::Array.
150

BUGS

152        use Array::Unique;
153        tie @a, 'Array::Unique';
154
155        @c = @a = qw(a b c a d e f b);
156
157        @c will contain the same as @a AND two undefs at the end because
158        @c you get the same length as the right most list.
159

TODO

161       Test:
162
163       Change size of the array Elements with false values ('', '0', 0)
164
165          splice:
166          splice @a;
167          splice @a,  3;
168          splice @a, -3;
169          splice @a,  3,  5;
170          splice @a,  3, -5;
171          splice @a, -3,  5;
172          splice @a, -3, -5;
173          splice @a,  ?,  ?, @b;
174
175       Benchmark speed
176
177       Add faster functions that don't check uniqueness so if I know part of
178       the data that comes from a unique source then I can speed up the
179       process, In short shoot myself in the leg.
180
181       Enable optional compare with other functions
182
183       Write even better implementations.
184

AUTHOR

186       Gabor Szabo <gabor@pti.co.il>
187

LICENSE

189       Copyright (C) 2002-2008 Gabor Szabo <gabor@pti.co.il> All rights
190       reserved.  http://www.pti.co.il/
191
192       You may distribute under the terms of either the GNU General Public
193       License or the Artistic License, as specified in the Perl README file.
194
195       No WARRANTY whatsoever.
196

CREDITS

198        Thanks for suggestions and bug reports to
199        Szabo Balazs (dLux)
200        Shlomo Yona
201        Gaal Yahas
202        Jeff 'japhy' Pinyan
203        Werner Weichselberger
204

VERSION

206       Version: 0.08
207
208       Date:    2008 June 04
209
210
211
212perl v5.30.0                      2019-07-26                  Array::Unique(3)
Impressum