Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I use a hash as a set?

by vroom (His Eminence)
on Jan 19, 2000 at 00:23 UTC ( [id://2156]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How do I use a hash as a set?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I use a hash as a set?
by vroom (His Eminence) on Jan 19, 2000 at 00:25 UTC
    You simply have a hash where each element in the set represents a key and its corresponding value is 1 or some other true value.
    my (%fruits,%vegetables); $fruit{apple}=1; $fruit{orange}=1; $fruit{banana}=1; $vegetable{corn}=1; $vegetable{carrot}=1; #test whether apple is in the set fruit if($fruit{apple}){ #test for inclusion in a set print "apple is a fruit"; }
Re: How do I use a hash as a set?
by swiftone (Curate) on May 31, 2000 at 23:14 UTC
    Using the answer above, here are some "quick" (to the programmer) ways to initalize a set:

    Using a hash slice

    foreach (@fruit{'apple','banana','plum'}){$_=1}
    This method works best when your set will not change, because you (pretty much) have to hardcode this string in.
    That is:
    #This will NOT work as intended $value="'apple','banana','plum'"; foreach (@fruit{$value}){$_=1} #doesn't work!

    Often you will not know the set you want at write-time, so you will want to be able to push and pop the set from an array. Converting an array into a set can be done like:

    @array=("apple","banana"); push @array, "plum"; foreach (@array){$fruit{$_}=1}
    These foreach loops can obfuscate your code, so a simple
    foreach (@array){$fruit{$_}=1} # initialize fruit set
    can greatly increase readability of code while not complicating your programming efforts
Re: How do I use a hash as a set?
by ariels (Curate) on Nov 29, 2000 at 17:35 UTC
    No need to store an actual 1 value in your hash. This works just fine:
      my %fruit;
      @fruit{qw/apple orange banana/} = ();
      warn "I know how to defend myself\n"
        if exists $fruit{banana};
    
    Note that the actual values stored here are undef. But in any case we just want to know if the key is there or not.
Re: How do I use a hash as a set?
by CharlesClarkson (Curate) on Jun 11, 2001 at 14:07 UTC

    The repeat operator (x) can be used with hash slices to get an easy to read set representation:

    my @fruit_set = qw/apple banana orange mango/; my @vegie_set = qw/onion tomato lettuce eggplant/; my (%fruit, %vegetable); @fruit{@fruit_set} = (1) x @fruit_set; @vegetable{@vegie_set} = (1) x @vegie_set;
Re: How do I use a hash as a set?
by Anonymous Monk on Sep 08, 2000 at 09:36 UTC

    Re: How do I use a hash as a set?

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-23 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found