Well, the first step is, of course, to try the code, and see what the result is. I'm modifying the hash VALUES slightly so you can better see the effect.
%hash = map +(++$i % 2 ? lc($_) : $_), (foo => 'BAR', This => 'thAT', THESE => 'Those'); for (keys %hash) { print "$_ => $hash{$_}\n" } __END__ this => thAT these => Those foo => BAR
As you can see by the output, our hash keys are all in lowercase, while the values have not been modified. So let me explain what my code does. It turns out, I don't need the +(...) around that first argument to map().
%hash = map # the + before the parenthesis was a way of # disambiguating to Perl that the (...) is not the list # of arguments I'm passing to map(). but I don't need it ++$i % 2 ? # first increment $i... lc($_) : # if $i % 2 is 1, then lowercase the string $_ # otherwise, keep it as it is , # then our hash initialization list as normal... ( foo => 'BAR', This => 'thAT', THESE => 'Those', );
The ++$i % 2 trick is to determine if I'm on an odd-number element in the list I pass it (in human terms, not Perl terms -- the first element of a list is the 0th, but here, the first element makes the value of $i 1). If ++$i % 2 returns 1, that means $i is odd, and that means it represents a hash KEY, which should be lowercase. If the value is 0, then we're looking at a hash VALUE, and we don't want to touch/modify it.

In reply to RE: RE: RE: Re: Case insensitivity in a hash... Futile effort? by japhy
in thread Case insensitivity in a hash... Futile effort? by ChuckularOne

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.