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