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

Of course, by far the easiest option is to lc() your keys before you insert them, and before you perform lookups in the hash...

Brian

Replies are listed 'Best First'.
RE: Re: Case insensitivity in a hash... Futile effort?
by japhy (Canon) on Apr 19, 2000 at 07:05 UTC
    Heh, then look at this :)
    %hash = map +(++$i % 2 ? lc($_) : $_), (foo => 'bar', This => 'that', THESE => 'those');
    Nifty-ish, I think.
      WHOA!

      Care to explain? I'm not even an acolyte yet! As a matter of fact, I've gotta go to perlfunc and see what this map guy does... :o)

      Cheers!
        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.