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 +(++$i % 2 ? lc($_) : $_), (foo => 'BAR', This => 'thAT', THESE => 'Those'); for (keys %hash) { print "$_ => $hash{$_}\n" } __END__ this => thAT these => Those foo => BAR
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.%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', );
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |