Krishna
Krishna

Reputation: 53

Perl Hash of Hashes

I am new to Perl, and I am trying something with a hash. I have a hash of hashes like below:

%HoH =   
(
    "Test1" => { checked => 1, mycmd => run1 },
    "Test2" => { checked => 1, mycmd => run2 },
)

Using the below code I will get the output given below:

for $family ( keys %HoH ) 
{
    print "$family: ";
    for $role ( keys %{ $HoH{$family} } ) 
    {
        print "$role=$HoH{$family}{$role} ";
    }
    print "\n";
}

Output:

Test1: checked=1 mycmd=run1 
Test2: checked=1 mycmd=run2

My question is, how can I access individual checked & cmd separately? By accessing separately, I can compare what is checked and do my task.

Upvotes: 1

Views: 263

Answers (2)

dolmen
dolmen

Reputation: 8714

for my $family ( keys %HoH )
{
    if ($HoH{$family}->{checked}) {
         # Do what you want...
    }
}

Upvotes: 1

dlamblin
dlamblin

Reputation: 45381

It's pretty straight-forward to just use the keyword(s) literally:

%HoH =   
(
    "Test1" => { checked => 1, cmd => run1 },
    "Test2" => { checked => 1, cmd => run2 },
);
if ($HoH{"Test1"}{checked}) {
print "Test1 is Checked with cmd: " . $HoH{"Test1"}{cmd} . "\n";
}

Test1 is Checked with cmd: run1

Did I understand your question correctly?

Upvotes: 3

Related Questions