This looks like a perfect solution. However it does require you to download and install a module from CPAN

My first advice would be to go out of your way to get CPAN.pm working; it is *massively* convenient to be able to solve problems quickly by just grabbing modules from CPAN.

However, if the OP needs to solve the stated problem without a CPAN module, he should use a case-folded key but store the original case as part of the value. In the example below each value is an anonymous array containing the original (non-case-folded) key and the actual desired value. It would be possible to structure the data in a different way (e.g., to use parallel hashes to store the Keys and the values), but this is one good way.

my @mixedcasekeys = ('ABC', 'foo', 'ExAbbrev'); sub value { return "This is the value for $_\n"; } my %hash; for (@mixedcasekeys) { $hash{lc $_} = [$_, value($_)]; } showkeys(); print "Adding key: 'Foo'\n"; $hash{lc 'Foo'} = ['Foo', value('Foo')]; showkeys(); print "Looping over the keys and showing the values:\n"; for $k (sort map {$hash{$_}[0]} keys %hash) { my $v = $hash{lc $k}[1]; print "$k => $v\n"; } sub showkeys { print "'Keys' in mixed case: " . (join ' ', map {$hash{$_}[0]} keys %hash) . "\n"; }

As you can see, solving little problems like this by hand is just a bit more complicated than grabbing a module off of the CPAN. Each and every time you have a little problem like this, solving it by hand will be more complicated than grabbing a module off of the CPAN. Getting CPAN.pm set up once is more complex than solving one or two such problems, but if you have Perl stuff to do in the future, CPAN will pay you back for your trouble quite quickly.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: Case insensitive hash keys by jonadab
in thread Case insensitive hash keys by kam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.