Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I represent sets in Perl?

Tags:

hash

set

perl

I would like to represent a set in Perl. What I usually do is using a hash with some dummy value, e.g.:

my %hash=();
$hash{"element1"}=1;
$hash{"element5"}=1;

Then use if (defined $hash{$element_name}) to decide whether an element is in the set.

Is this a common practice? Any suggestions on improving this?

Also, should I use defined or exists?

Thank you

like image 760
David B Avatar asked Sep 13 '10 11:09

David B


3 Answers

Yes, building hash sets that way is a common idiom. Note that:

my @keys = qw/a b c d/;
my %hash;
@hash{@keys} = ();

is preferable to using 1 as the value because undef takes up significantly less space. This also forces you to uses exists (which is the right choice anyway).

like image 172
Chas. Owens Avatar answered Nov 15 '22 16:11

Chas. Owens


Use one of the many Set modules on CPAN. Judging from your example, Set::Light or Set::Scalar seem appropriate.


I can defend this advice with the usual arguments pro CPAN (disregarding possible synergy effects).

  1. How can we know that look-up is all that is needed, both now and in the future? Experience teaches that even the simplest programs expand and sprawl. Using a module would anticipate that.
  2. An API is much nicer for maintenance, or people who need to read and understand the code in general, than an ad-hoc implementation as it allows to think about partial problems at different levels of abstraction.
  3. Related to that, if it turns out that the overhead is undesirable, it is easy to go from a module to a simple by removing indirections or paring data structures and source code. But on the other hand, if one would need more features, it is moderately more difficult to achieve the other way around.
  4. CPAN modules are already tested and to some extent thoroughly debugged, perhaps also the API underwent improvement steps over the time, whereas with ad-hoc, programmers usually implement the first design that comes to mind.

Rarely it turns out that picking a module at the beginning is the wrong choice.

like image 15
daxim Avatar answered Nov 15 '22 16:11

daxim


That's how I've always done it. I would tend to use exists rather than defined but they should both work in this context.

like image 2
Stephen Darlington Avatar answered Nov 15 '22 16:11

Stephen Darlington