for (grep $tree{$_}, keys %tree) { my @g = $_; push @g, $_ while $_=$tree{$_}; print(join(':', @g), "\n"); }