Just because a hash stores key/value pairs doesn't mean that you need to put anything useful in the value.

C++ comes with std::unordered_set and std::unordered_map. The first one is designed for just a set of keys. The second is a set of keys that map to values. The second one is more like Perl's hashes. But the fact that the two different containers exist is mostly a matter of memory efficiency and semantic purity.

Now back to Perl: Your hashes won't be growing too big, so memory efficiency probably isn't an issue, and we don't need to be too particular about semantic purity. Hashes often may be used where you might think in terms of sets.

my %heros; @heros{ qw( thetick wonderwoman batman superman spiderman ) } = (); print "Yes!\n" if exists $heros{thetick};

In the code above we're creating a hash called %heros that contains elements named for various superheros. But we don't explicitly assign a value to each of the elements. Their value is undefined, and it really doesn't matter because we never use it. Later we test to see if 'thetick' is among our set of superheros.


Dave


In reply to Re: Trying to understand hashes (in general) by davido
in thread Trying to understand hashes (in general) by james28909

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.