http://qs321.pair.com?node_id=1204035


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.