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

I guess you've already considered looping? What about this:
foreach $key (keys %food_color) { if (uc($key) eq uc($new_key)) { $food_color{$key} = $new_value; } }
Of course I haven't tested this, but I don't see why it wouldn't work. With uc() you're not really ignoring case, but uppercasing everything just to make sure they match. (kinda like an SQL approach). Le'me know if that works!

Cheers!

#!/home/bbq/bin/perl
# Trust no1!

Replies are listed 'Best First'.
RE: Re: Case insensitivity in a hash... Futile effort?
by btrott (Parson) on Apr 18, 2000 at 23:49 UTC
    Hmm. Well, TMTOWTDI, but I really wouldn't use this if my hash were even slightly large; why use a hash at all if you're going to loop through each key every time you need to do an insert? You'd be better off using a sorted array that you could do a binary search on, or something such.

    This is particularly bad if the hash is multi-dimensional. If I wanted to do the equivalent of

    $hash{foo}{bar}{baz} = 2;
    using your looping method, I'd have to loop over *three* arrays (for the three different keys).

    I'm not saying you're wrong--I'm just saying that the tie method is, in most cases, a much better and more general solution.

      I was going to say I wasn't looping through the hash, but then I went back and looked at the code because I thought... "How could it be working?" And I am. The hash won't be too terribly large for this application maybe 200 records.
      I guess it's time to learn to use tie.

      Your humble servant,
      -Chuck
RE: Re: Case insensitivity in a hash... Futile effort?
by ChuckularOne (Prior) on Apr 18, 2000 at 23:36 UTC
    Thank you
    Thank you
    Thank you
    Thank you
    Thank you
    Thank you
    Thank you
    Thank you

    This was enough to take care of my issue.

    Your humble servant,
    -Chuck
      Chuck,

      1. You're more than welcome (x8)! :o)
      2. Btrott has a VERY good point! If you're dealing with a big hash you could get slugish in NO time.

      But then again, (I shall say this perl style)
      if (you're running a big hash) { it's probably because you're pulling something off from a text file; if (that's true) { then your text file shouldn't be too big anyway; } else { its going to get slugish from the filesystem access in the first place!; } } else { you should be considering a comercial database product, under which you wouldn't need to do case detection on your hash; }

      Was that reasonably readable? In any case, I'm glad I could be of assistance!

      Cheers again!