in reply to Testing for key/value match

It seems to me you're misunderstanding how hashes work.
Keys in a hash are unique. What this means is that you can't have two or more identical keys with different values.
If you want to test whether a certain key exists and has the value you expect, you can do this
if (defined $foo{'bar'} and $foo{'bar'} eq 'baz'){ }
--perlplexer

Replies are listed 'Best First'.
Re: Re: Testing for key/value match
by particle (Vicar) on May 10, 2002 at 15:01 UTC
    If you want to test whether a certain key exists and has the value you expect, you can do this

    if (defined $foo{'bar'} and $foo{'bar'} eq 'baz'){ }
    perhaps you mean exists?

    if (exists $foo{'bar'} and $foo{'bar'} eq 'baz'){ }
    reminder to all: in an array or hash lookup, defined tests for definedness of the value of the array index or hash key, which may not be what you mean. use exists to test whether the array index or hash key is present.

    ~Particle *accelerates*

      No, defined() is what I wanted to say.
      The value may exist but may be undefined, in which case the 'eq' will produce a warning.

      --perlplexer
        so if undefined is what you expect, it looks like none of these solutions will work.

        ~Particle *accelerates*