----- Original Message -----
From: "Marcos Lorenzo de Santiago" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 12, 2002 11:59 AM
Subject: can I get the keys from a hash giving the value?


> I got a hash:
>
> %week=("monday" => 1,
>        "tuesday" => 2,
>        "wednesday" => 3,
>        "thursday" => 4,
>        "friday" => 5,
>        "saturday" => 6,
>        "sunday" => 7
> );
>
> and I have $day=1, can I get then "monday"??

based on the fact a hash can be treated as an array, you can do:

my %rev_week = reverse %week;
print $rev_week{1} . "\n";

be careful, this only works if there is a One to Many mapping between keys &
values
e.g. if:
%hash = (
        "apple" => 1,
        "orange" => 2
        "smallorange" => 2
);

%rev_hash = reverse %hash
print $rev_hash{2}

would it print orange OR smallorange? (who can say which one! (there is a
'magical' rule ))

although thats not a problem here.


_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to