in reply to case-insensitive hash keys

The simplest way is to just force every use to lower-case:

my %assoc = (); $assoc{lc('TheKey')} = 'test'; print $assoc{lc('TheKEY')}, $/;

(Added) Take a look at Hash::Case. It has Hash::Case::Lower, which uses a tied hash to enforce lower case keys. Sounds like just what you want.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: case-insensitive hash keys
by ManFromNeptune (Scribe) on Aug 21, 2004 at 00:17 UTC
    Good idea, but this would be as a modification to a set of Perl scripts that use hashes extensively, with approx. 20,000 lines of code. :S So, I guess I'm looking for something that doesn't require that much modification as inserting the lc() function everywhere.
      ManFromNeptune,
      Hash::Case::Preserve, in the same family as the module suggested by Zaxo, does case insensitive keys while maintaining the original case. The thing is you have to tie the hash using this module. There is no magic way to say "treat all hashes in this program as case insensitive".

      Cheers - L~R

        Ok, this is a potential solution. However (final neurosis question), I'm concerned about speed. Alot of tied hashes are going to be created in the context of a single web hit. Isn't that going to be slower than using regular hashes? (I suppose this is a classic tradeoff issue: raw speed vs. maintainability.)

        thanks all,
        MFN

      I have not thought this through completely, but you could use a tied hash which on STORE used a lowercase function. This would alleviate the need to have a lc() all over the code but it would slow your code down a bit. Again, this is off the top of my head so it might not work.