justkar4u has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Well I am trying to use a hash table for returning value when a specific key is supplied. My question is, is it possible to assign two values for a same key ?(I know that 2 keys can have same values but vice versa is not true,right?) I tried to do this using this line of code.
my %table= ( "optimal", "good", "failed", "missing,bad"); print $table{failed};
The output is missing,bad To be more clear on what I am trying to do, I would like to extract one value out of "missing,bad" if a particular condition is true. I am planning to parse it for the , delimiter and then store the prefix or suffix(which i believe is too clumsy). I wanted to know if there is a better way of doing this/suggestions ?. - Thanks Ram

Replies are listed 'Best First'.
Re: Having multiple values for a Hash key
by wind (Priest) on Apr 30, 2011 at 01:23 UTC
    Use a hash within a hash
    my %table = ( optimal => 'good', failed => {missing => 1, bad => 1}, ); print $table{failed}{missing};
    Or an array within the hash
    my %table = ( optimal => 'good', failed => [qw(missing bad)], ); print (grep {$_ eq 'missing'} @{$table{failed}}) ? 'missing' : 'ok';
      Hi , Thanks everyone for the reply.. I am using the solution suggested by wind since it matches closely with what i need to implement ! Thank you again for the replies.
Re: Having multiple values for a Hash key
by Anonymous Monk on Apr 30, 2011 at 01:27 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.