Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: HTML::Template and hashes of hashes

by blokhead (Monsignor)
on Mar 16, 2004 at 02:17 UTC ( #336905=note: print w/ replies, xml ) Need Help??


in reply to HTML::Template and hashes of hashes

This recursively flattens multilevel hashes so you can get to them with a TT-ish syntax from HTML::Template. It should be straight-forward to modify if you also want arrayrefs to be recursively flattened too.

sub flatten { my $hr = shift; my %result; while (my ($k, $v) = each %$hr) { if (ref $v eq 'HASH') { my $r = flatten($v); $result{"$k.$_"} = $r->{$_} for keys %$r; } else { $result{$k} = $v; } } \%result; } print Dumper flatten { foo => { in_foo => 1, bar => 1, baz => { in_baz => 1 } }, blah => 1 }; __END__ $VAR1 = { 'foo.bar' => 1, 'blah' => 1, 'foo.baz.in_baz' => 1, 'foo.in_foo' => 1 };

blokhead


Comment on Re: HTML::Template and hashes of hashes
Download Code
Replies are listed 'Best First'.
Re: Re: HTML::Template and hashes of hashes
by jdtoronto (Prior) on Mar 16, 2004 at 16:06 UTC
    Thanks blokhead,

    Thats just the trick. I was pondering the same answer when I saw your response and I took your code and used it. Thank you so much, it did just what I need.

    jdtoronto

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2015-07-07 20: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 (93 votes), past polls