Buddha Buck wrote:
> 
> In a hash implementation, your hash keys -are- your set elements!
> 
> my %set;
> 
> # add elements to %set
> $set{'elem1','elem2'} = 1;
> 
> # Compute union
> $union{keys %set1, keys %set2} = 1;

Oh, yeah, using native hashes for sets -- what could be simpler?

(Hint:
  @set{'elem1','elem2'} = ();
  @union{keys %set1, keys %set2} = ();
)


> # Compute intersection
> for $elem (keys %set1) { $intersect{$elem} = 1 if exists($set2{$elem});}

  @intersection = grep { exists $set1{$_} } keys %set2;


And this, to me, illustrates why the pure hash syntax is not
entirely optimal for sets.  Here we've computed the intersection
set of keys in one neat stroke -- and we're left with an array,
not a hash.  To get it into a hash, we'd have to do something
like

  %intersection = map { exists $set1{$_} ? ( $_ => 1 ) : () } keys %set2;

or the somewhat more efficient

  %intersection=();
  @intersection{ grep { exists $set1{$_} } keys %set2 } = ();

If it were possible to assign to the keys of a hash, we'd be 
a lot closer to our ideal:

  keys(%intersection) = map { exists $set1{$_} ? ( $_ => 1 ) : () } keys %set2;

but this is not currently legal perl.

-- 
John Porter

        We're building the house of the future together.

Reply via email to