in reply to case-insensitive hash keys

Well, if you really don't care about whether the keys remain in their original case or not, you can do something like this (untested):
$assoc{lc $_} = delete $assoc{$_} for keys %assoc;
This will convert all keys to lower case. Then to check, you make sure the key is lc()ed:
print $assoc{lc 'theKEY'} . "\n";
HTH.

Replies are listed 'Best First'.
Re^2: case-insensitive hash keys
by ManFromNeptune (Scribe) on Aug 22, 2004 at 02:47 UTC

    Maybe I'm missing something here, but it seems to me that this would be a good Perl intepreter flag... i.e., that all associate array keys should be treated case-insensitively. The interpreter would implicitly run "lc" when computing the hash value for storing or retrieving a value.

    The background here, as I alluded to above, is that I've got about 20,000 lines of code for a web system. Alot of my application is database-driven, and I have a module that just contains functions to retrieve rows from numerous tables.

    The mechanism for returning data from the retrieve functions is a pointer to a hash table (of the returned row). The keys in the hash are case-sensitive names of the fields. So the risk here is that someone who maintains this code makes a change and typos a hash key. Unlike with using strict vars, where Perl warns you if it thinks you've made a typo in a variable name, it would be very difficult to track down a typo in a hash key.

    Any other thoughts?

    thanks
    MFN

      How many functions are there return hashrefs? Is it a great big deal to tie the hashes before filling and returning them?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        About 60 functions. So its a good chunk of work, but probably worth it for the maintainability. What's the efficiency/speed of using tied hashes vs. regular hashes? (this is for a high-volume web application.) thanks MFN