Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I use a hash as a set?

( #2156=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 19, 2000 at 00:23 UTC
Q&A  > hashes


Answer: How do I use a hash as a set?
contributed by swiftone

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
Answer: How do I use a hash as a set?
contributed by vroom

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"; }
Answer: How do I use a hash as a set?
contributed by ariels

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.
Answer: How do I use a hash as a set?
contributed by CharlesClarkson

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;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2015-07-08 02:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls