Return to Snippet

Revision: 17804
at September 14, 2009 16:36 by deepsoul


Initial Code
my @ary= qw( foo bar bla ack 42 foo );

my %set;
@set{@ary}= (1) x @ary;
# by the way: keys %set is now an array containing the elements
# of @ary without duplicates! (though possibly shuffled)

# or right during initialisation:
my %set2= map { $_ => 1; } @ary;

Initial URL


Initial Description
Perl hash sets are hashes used to represent sets.  Their keys are the set elements, and the corresponding values are just 1.  For keys that are not set elements, the hash will return undef, equivalent to false.

Adding a whole array to a hash set is often useful, but how to do it is slightly tricky and not widely known.  Here goes...

Initial Title
Add array elements to hash set

Initial Tags
list, array

Initial Language
Perl