At 03:40 PM 9/1/00 +0200, Gael Pegliasco wrote:
> >
> >     Arrays are ordered.
> >     Hashes are not.
> >     Neither are sets.
> >
> >     Arrays can have repetitions.
> >     Hashes can not.
> >     Neither can sets.
> >
> > etc.
> >
> > --tom
>
>
>Yes, this is true, but the natural syntax, for me, to manipulate sets,
>is the one of arrays.
>
>It is not natural to write :
>
>%my_fruit_set = ( 1 => 'orange', 2 => 'lemon' );

You are right, that is not natural.  I don't know why I would ever want to 
use a hash keyed using integers instead of arrays.  But that's not how I 
would use  a hash for a set.


>but it is natural to write :
>
>@my_fruit_set = ( 'orange', 'lemon' );
>
>I don't want to have to deal with keys of hashes because my set elements
>don't care about them !

In a hash implementation, your hash keys -are- your set elements!

my %set;

# add elements to %set
$set{'elem1','elem2'} = 1;

# test for set memebership
print "elem1 in %set\n" if exists $set{'elem1};

# Compute union
$union{keys %set1, keys %set2} = 1;

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

# set difference
delete @set1{keys %set2};

How do you currently do these things with arrays?


>so, I want new functions to manipulate arrays.
>
>then, when manipulating arrays, without speaking about sets, you often
>need in/union/intersection functions, even if arrays have duplicated
>elements or if they are ordered.
>
>Well, in fact I don't understand why you don't think that
>union/intersection/difference are not usefull functions for arrays ?

Because I don't know what they will do with an array.  They are 
(reasonably) well defined for sets, but not for arrays.


>Gael,

Reply via email to