Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Ignore Case when comparing Hash Keys

by ELISHEVA (Prior)
on Nov 15, 2010 at 19:12 UTC ( #871546=note: print w/replies, xml ) Need Help??


in reply to Ignore Case when comparing Hash Keys

exists can only do case sensitive comparisons. Instead of doing exists, extract the keys from both hashes using keys, and then lower case them both before comparison:

Note: this solution preserves the case in both hashes by storing keys in a temporary third hash.

# make temporary hash using lower case keys only # hash allows for fast lookup of matching key later on in # our for loop my %hashLowercaseKeys1 = ( map { lc($_) => 1 } keys %hash1 ); foreach my $k (keys %hash2) { if (exists $hashLowercaseKeys1{lc($k)}) { print "$k: matched\n"; } else { print "$k: no match\n"; } }

Replies are listed 'Best First'.
Re^2: Ignore Case when comparing Hash Keys
by avidcoder (Novice) on Nov 15, 2010 at 19:59 UTC
    Thanks a lot. This works.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://871546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2022-12-10 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?