John Porter wrote:
>
> 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.
Ok, Ok, you're all very skilled perl developpers, much more skilled than
am I.
But, Do you really think that all these ingenuities, to not use another
term, are really natural and easy to understand to novice programmers ?
Sorry, but I still thinking that the solution of native perl functions
still the simplest and easiest solution.
Perl is simple, perl is easy, perl is quite natural. I think my ask is
good for perl ! ;o)>
I think that all your solutions are cool and powerfull solutions to
solve my problem, and that they are also cool to show how much the
language is powerfull and how you can do so simple things in so several
ways, even if the language doesn't offers such simple functions, but I
DON'T THINK THAT IS SIMPLE AND NATURAL ! :(
Of course, all this is said with joke, but is still what I think. :)
Gael,