Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Arbitrarily Nested HoH

by BrowserUk (Patriarch)
on Oct 15, 2010 at 04:44 UTC ( [id://865400]=note: print w/replies, xml ) Need Help??


in reply to Arbitrarily Nested HoH

Probably overkill for what you want, but it handles a mix of nested hashes and arrays, and a few other refs as well.

sub traverse(&$) { my( $code, $ref ) = @_; my $d; $d = { SCALAR => sub{ my $code = shift; my $ref = shift; return $code->( @_, $$ref ); }, ARRAY => sub { my $code = shift; my $ref = shift; map $d->{ ref $ref->[ $_ ] }->( $code, $ref->[ $_ ], @_, $ +_ ), 0 .. $#$ref; }, HASH => sub { my $code = shift; my $ref = shift; map $d->{ ref $ref->{ $_ } }->( $code, $ref->{ $_ }, @_, $ +_ ), keys %$ref; }, REF => sub{ my $code = shift; my $ref = shift; return $d->{ ref $$ref }->( $code, $$ref, @_ ); }, CODE => sub{ my $code = shift; my $ref = shift; return $d->{ '' }->( $code, "$ref", @_ ); }, '' => sub{ my $code = shift; my $ref = shift; return $code->( @_, $ref ); }, }; return $d->{ ref $ref }->( $code, $ref ); } traverse { print join ',', @_ } \%hash;

You can also use it like this to build an array:

my @strings = traverse{ join',', @_ } \%HorAofHorA;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found