I am trying to iterate over items in the data structure that uses nested hash.
To do so, I wanted to see what keys are in there.
The following is what I have tried. But I am getting an error
my %tgs = (
'articles' => {
'vim' => 'about vim',
'awk' => 'about awk',
'sed' => 'about sed'
},
'ebooks' => {
'linux 101' => 'about linux',
}
);
foreach my $k (keys %tgs){
print $k;
print "\n";
foreach my $k2 (keys %$tgs{$k}){ #<-----this is where perl is having a problem
print $k2;
print "\n";
}
}
syntax error at PATH line #, near "$tgs{"
syntax error at PATH line #, near "}"
Execution of PATH aborted due to compilation errors.
What’s is wrong with my approach? My reasoning was since $tgs{$k} returns the reference of hash, I could dereference it in for each loop, but I guess not?
You need braces around
$tgs{$k}:The full code would be: