Re: How do I find the key of a specific hash element?

[email protected] ("Dr.Ruud")
Newsgroups perl.beginners
Organization Chaos rules.
Message-ID <[email protected]>
jshock schreef:
> For example:
> 
> my %weekdays = (
> 0  => "SUN",
> 1 => "MON",
> 2 => "TUE",
> 3 => "WED",
> 4 => "THU",
> 5 => "FRI",
> 6 => "SAT",
> );
> $weekdays{2}; # gives "TUE"
> 
> But what if I know "TUE" and want to find out what the key is? Is
> there a construct like $weekdays{"TUE"} that gives 2"

The data structure that you show, belongs in an array. 

perl -le'
#!/usr/bin/perl
  use strict;
  use warnings;

  my @n2d = qw( SUN MON TUE WED THU FRI SAT );
  my %d2n; @d2n{ @n2d } = ( 0 .. $#n2d );

  print $d2n{ WED };
'
3

-- 
Affijn, Ruud

"Gewoon is een tijger."
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.