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.
$HoH{"Test1"}{checked}
what you want? - Karthik Tif ($HoH{Test1}->{checked}) { system($HoH{Test1}{cmd}) }
? - Charles Engelkeuse strict;
anduse warnings;
and fix anything they complain about. It will make your code cleaner and easier to debug. (In particular, you should add quotes around the stringsrun1
andrun2
and declare your loop variables as local withfor my $var (...)
.) - Ilmari Karonen