in reply to How to flatten a spindly tree?

Without thinking too deeply on optimizations or better data structures this code seems to do the trick
my $old = { this => { is => { the => { day => {}, hour => {}, }, this => {}, }, }, that => { that => { is => { is => {}, not => { is => { not => {}, }, }, }, }, }, those => { were => { the => { days => {}, }, }, }, }; use strict; sub splat { my $tree = shift; my $ret = {}; while(my($k,$v) = each %$tree) { if(keys %$v == 1) { my $onekey = (keys %$v)[0]; my $newkey = "$k $onekey"; while( keys %{$v = $v->{$onekey}} == 1 ) { $onekey = (keys %$v)[0]; $newkey .= " $onekey"; } $ret->{$newkey} = splat($v); } else { $ret->{$k} = splat($v); } } return $ret; } sub dumptree { my($t,$d) = ( $_[0], ($_[1] || 0) ); print " " x $d, $_,$/ and dumptree($t->{$_}, $d + 1) for sort keys %$t; } use Data::Dumper; my $new = splat $old; print Dumper $new; dumptree $new; __output__ $VAR1 = { 'those were the days' => {}, 'that that is' => { 'not is not' => {}, 'is' => {} }, 'this is' => { 'this' => {}, 'the' => { 'day' => {}, 'hour' => {} } } }; that that is is not is not this is the day hour this those were the days
So that neatly flattens all nodes with only a single child into flattened string as the key.
HTH

_________
broquaint