Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Hashes Question

by ColonelPanic (Friar)
on Nov 30, 2012 at 08:36 UTC ( #1006427=note: print w/ replies, xml ) Need Help??


in reply to Hashes Question

Here is an example of how you could recurse through a structure of hash references in practice.

It uses the ref function to determine whether a given level is a hash reference.

use Modern::Perl; my %ref_chain = ( 'key1' => { 'key2' => { 'key3' => 'not a reference' } }, 'key4' => { 'key5' => 'no ref here' } ); sub traverse_hashes { my $hashref = shift; foreach my $key (keys %$hashref) { if (ref $hashref->{$key} eq 'HASH') { say "$key is a hash ref. Recursing."; traverse_hashes($hashref->{$key}); } else { say "$key is not a hash ref (value: $hashref->{$key})"; } } } traverse_hashes(\%ref_chain);


When's the last time you used duct tape on a duct? --Larry Wall


Comment on Re: Hashes Question
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2015-07-29 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls