Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Case insensitive hash key existance

by kennethk (Abbot)
on Nov 22, 2017 at 16:00 UTC ( [id://1204035]=note: print w/replies, xml ) Need Help??


in reply to Case insensitive hash key existance

If you have an existing hash (thus fc is not an option), you can grep the key set against a case-insensitive regular expression:
if(grep /^\Q$str\E$/i, keys %data) { # case-insensitive exists
This solves an immediate problem, though making sure your keys were well-formed in the first place would probably be cleaner.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Case insensitive hash key existance
by Your Mother (Archbishop) on Nov 22, 2017 at 16:51 UTC

    :P

    if ( grep fc $str eq fc $_, keys %data ) { # case-insensitive exists

    Or to "fix" the data-

    $data{fc$_} = delete $data{$_} for keys %data;

    As this example should make clear, and haukex's links explained, the entire operation is probably a bad idea as it is destructive (or at least has the appearance of being so if the plain folding is done without deleting.

Re^2: Case insensitive hash key existance
by merrymonk (Hermit) on Nov 22, 2017 at 16:31 UTC
    Thanks for all the replies. I take the point about well formed data for keys and normally I would not want to do such a thing.
    However, there are reasons why it is OK in this case.
    The grep suggestion does just what I want.

      Be warned of the massive performance hit. It replaces an O(1) lookup with than O(N) lookup. That's not good, but it's not so bad ...except that this is likely used in another another loop. Then that O(N) or O(N^2) loop because O(N^2) or O(N^3), and things start to crawl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found