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.

One approach I've used is to maintain an array that is reviewed for the match using a "foreach" type iteration.

Another approach is to maintain a hash, and test for the presence of a hash value that matches.

It seems that using the second approach is more efficient from both an operating system standpoint, and a coding standpoint:
foreach (@whatever) { if (/^does this match$/) { do something; } }
Vs.
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', )
It feels like there's something wrong (or maybe more accurately, inefficient) using a hash this way, but I'm not smart enough to know if that is in fact true.

When I use hashes as described above, things seem to work OK ... I'm just wondering/hoping that some of the brainier participants here might be able to point out errors in my thinking, and/or more appropriate ways of how to handle this type of situation.


In reply to 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.