tags:

views:

110

answers:

6

I've hash of hash like this:

$hashtest{ 1 } = {
  0 => "A",
  1 => "B",
  2 => "C"
};

For example, how can I take the value of B of hash{ 1 }?

$hashtest{'B'}{1}
A: 
$hashtest{1}{1};
But if I know the value and I want the index? For example I want know the "index" of 'A' of the hash{ 1 }?
+3  A: 

Others have provided the proverbial fish

Perl has free online (and at your command prompt) documentation. Here are some relevant links:

perldoc perlreftut

perldoc perldsc

References Quick Reference (PerlMonks)

toolic
+1  A: 
$hashtest{ 1 } = { 0 => "A", 1 => "B", 2 => "C" };

my $index;
my $find = "B";
foreach my $key (keys %{ $hashtest{1} }) {
    if($hashtest{1}{$key} eq $find) {
        $index = $key;
        last;
    }
}

print "$find $index\n";
bob.faist
+3  A: 
M42
A: 

Since you have used numbers for your hash keys, in my opinion, you should be using an array instead. Else, when reversing the hash, you will lose duplicate keys.

Sample code:

use strict;
use warnings;

use List::MoreUtils 'first_index';

my $find  = 'A';
my @array = qw{ A B C };
my $index = first_index { $_ eq $find } @array;

Perl Data Structures Cookbook will help you understand the data structures in Perl.

Alan Haggai Alavi
A: 

If all of your keys are integers, you most probably want to deal with arrays and not hashes:

$array[1] = [ qw( A B C ) ];  # Another way of saying [ 'A', 'B', 'C' ]

print $array[1][1];           # prints 'B'
Zaid