in reply to Creating lists using a hash with the same key/values
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.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 simpleAssuming 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:if (/$hash{$whatever}/) { do something; }%SampleHash ( 'whatever' => 'whatever', 'whoever' => 'whoever', )
will fail to see it. The strings that do that, are "" and "0".if($SampleHash{$string}){ ... }
I ordinarily use one of the following approaches:
orforeach(LIST) { $checked{$_}++; } if($checked{$string}) { ... }
but there are more. Use what seems most appropriate at the time.@checked{LIST} = (); if(exists $check{$string}) { ... }
|
|---|