You can try this to find out the depth of the Perl hash:
use strict;
my $hash_ref = {
'hashes' => {
'h1' => { 'klf' => '1', 'moomooland' => '1' },
'h2' => { 'klf' => '2', 'moomooland' => '2' },
'h3' => { 'klf' => '3', 'moomooland' => '3' }
},
'keys' => {
'k1' => 'key1',
'k2' => 'key2',
'k3' => 'key3'
}
};
print_nested_hash($hash_ref, 0);
sub print_nested_hash {
my $hash_ref = shift;
my $depth = shift;
foreach my $key (sort keys %{$hash_ref}) {
print ' ' x $depth, $key, "\n";
if (ref($hash_ref->{$key}) eq "HASH") {
print_nested_hash($hash_ref->{$key}, $depth+1);
}
}
}
CONCLUSION:
hashes
h1
klf
moomooland
h2
klf
moomooland
h3
klf
moomooland
keys
k1
k2
k3
source
share