Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi everyone,
Say you had an unknown hash structure and you didnt know how many dimensions to it there were. By dimension I mean child hashes, so it could be HoH, HoHoH, HoHoHoH, and so on, you just dont know.
How would you traverse this hash to print out all key => value pairs. The only way I know how is a series of for loops, however that would mean you have to know in advance how deep it goes.
Any help is greatly appreciated, thanks in advance, Mich.for my $k1 ( sort keys %{ $self->{"_dataSymbols"} } ) { print "$k1\n"; for my $k2 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 }} ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 }->{ + $k2 } } ) { print "\t\t$k3\n"; for my $k4 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 } +->{ $k2 }->{ $k3 } } ) { print "\t\t\t$k4\n"; for my $k5 ( sort keys %{ $self->{"_dataSymbols"}->{ $k +1 }->{ $k2 }->{ $k3 }->{ $k4 } } ) { print "\t\t\t\t$k5\n"; for my $k6 ( sort keys %{ $self->{"_dataSymbols"}->{ + $k1 }->{ $k2 }->{ $k3 }->{ $k4 }->{ $k5 } } ) { print "\t\t\t\t\t$k6 => %{ $self->{"_dataSymbols" +}->{ $k1 }->{ $k2 }->{ $k3 }->{ $k4 }->{ $k5 } }\n"; } } } } } }
|
---|
Back to
Seekers of Perl Wisdom