Often, when coding scripts, I encounter situations where I need to test to see if a variable matches an item that's stored in a list of values.
if (/$hash{$whatever}/) { do something; }
Assuming I'm thinking correctly so far, my next question has to do with creating the hash value. Is there anything "wrong" with populating a hash with keys and values that are the same? For example:
%SampleHash ( 'whatever' => 'whatever', 'whoever' => 'whoever', )
The idea to use a hash to check if an item can be found in a set, is a good one. What is wrong with your last approach, is that if the string evaluates to false, a simple
if($SampleHash{$string}){ ... }
will fail to see it. The strings that do that, are "" and "0".

I ordinarily use one of the following approaches:

foreach(LIST) { $checked{$_}++; } if($checked{$string}) { ... }
or
@checked{LIST} = (); if(exists $check{$string}) { ... }
but there are more. Use what seems most appropriate at the time.

In reply to Re: Creating lists using a hash with the same key/values by bart
in thread Creating lists using a hash with the same key/values by Hagbone

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.