I try to print hash key and value in tree form. my perl code is given below.
use strict ;
use warnings ;
my %hash = (
first => {
a => "one",
b => "two",
c => "three",
},
second => {
d => "four",
e => "five",
f => "six",
},
third => "word",
);
foreach my $line (keys %hash) {
print "$line: \n";
foreach my $elem (keys %{$hash{$line}}) {
print " $elem: " . $hash{$line}->{$elem} . "\n";
}
}
output error message: second: d: four f: six e: five third: Can't use string ("word") as a HASH ref while "strict refs" in use at C:\Users\Dell\Music\PerlPrac\pracHash\hshofhsh_net.pl line 19.
here, under third key value not print. how can i do it?
$hash{third}is not a hashref, so%{$hash{third}}is throwing an error. You can useif (ref $hash{$line} eq "HASH") ...- glenn jackman