tomazos has asked for the wisdom of the Perl Monks concerning the following question:
I think the best way is to use a hash to implement a set by using the existant keys to represent the current members of the set.
My current strategy is along the lines of:
my %set; sub contains($) { return exists $set{$_[0]} } sub add($) { $set{$_[0]} = 1 } sub all($) { return [keys %set] }
(I'm not actually using subs -- just for clarity.)
Is this approach the simplest way?
Is there a better approach avoiding the assignment for the add? Something like create $set{'foo'}?
I think I don't need the hash entries to be defined. I just need them to exist. (If you iterate over keys it will still return entries that have existant but undefined values right?)
-Andrew.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash Set
by ambrus (Abbot) on Sep 11, 2005 at 07:15 UTC | |
by parv (Parson) on Sep 11, 2005 at 07:49 UTC | |
by tomazos (Deacon) on Sep 11, 2005 at 07:56 UTC | |
|
Re: Hash Set
by parv (Parson) on Sep 11, 2005 at 08:05 UTC |