in reply to Case insensitivity in a hash... Futile effort?

Well, first of all, redefining the hash *would* get rid of Apple => red, but that's probably not what you meant. :)

In a thread a while back, vroom wrote a nice solution using tie: Re: case sensitivity in hashes.

You'll have to modify it a bit, because you want multi- dimensional hashes; so I think you'd probably just have to tie all of the hashes of hashes of hashes, etc. So, for example, say you're using vroom's Tie::CaseInsensitive:

use strict; my %HASH; use Tie::CaseInsensitive; tie %HASH, 'Tie::CaseInsensitive'; $HASH{BoB} = 1; $HASH{bob} = 2; $HASH{BOb} = 3; tie %{$HASH{bill}}, 'Tie::CaseInsensitive'; $HASH{bill}{jones} = 3; $HASH{BILL}{JONES} = 2; use Data::Dumper; print Dumper \%HASH;
You should get:
$VAR1 = { 'bill' => { 'jones' => 2 }, 'bob' => 3 };
which looks quite right.

Replies are listed 'Best First'.
RE: Re: Case insensitivity in a hash... Futile effort?
by ChuckularOne (Prior) on Apr 19, 2000 at 00:37 UTC
    So to tie a hash four dimensional hash, I would:
    $level1 = "LEVEL1"; $level2 = "LEVEL2"; $level3 = "LEVEL3"; $setting1 = "level1"; $setting2 = "level2"; $setting3 = "level3"; use Tie::CaseInsensitive; tie %{$HASH{$level1}{$level2}{level3}}, 'Tie::CaseInsensitive'; $HASH{$level1}{$level2}{level3} = 3; $HASH{$setting1}{$setting2}{setting3} = "Three"; use Data::Dumper; print Dumper \%HASH;
    I tried to run this code and got the following error:
    Can't locate Tie/CaseInsensitive.pm in @INC at ./hashTieTest.pl line 12.
    I'm guessing this means that I need to add a .pm .
    but which one? Is there a tie.pm? I'll go check CPAN. -Chuck
      Tie::CaseInsensitive can be gotten here: Re: case sensitivity in hashes.

      Just copy and paste it into your editor, save it, then put it somewhere in your @INC as Tie/CaseInsensitive.pm.

      And w/ a multi-dimensional hash, you need to tie each level of the hash. Does that make sense? So you tie the hash %HASH, then you tie $HASH{level1}, and so on down.

        I hate sounding ignorant, but once I know what I'm doing I promise to answer questions rather than just ask them.

        How do I add something to @INC. All my books are at work and I'm trying to get this done.

        Your humble servant,
        -Chuck
        Thanks a lot, It makes sense, but I don't think I'd have figured it out :-)
        -Chuck