I would like to represent a set in Perl. What I us开发者_运维技巧ually 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
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).
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).
- 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.
- 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.
- 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.
- 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.
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.
精彩评论