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
Use one of the many Set modules on CPAN. Judging from your example,
Set::LightorSet::Scalarseem appropriate.I can defend this advice with the usual arguments pro CPAN (disregarding possible synergy effects).
Rarely it turns out that picking a module at the beginning is the wrong choice.