in reply to Using Tree::Simple with while loop and input file.

You really don't need Tree::Simple to do that. A simple approach could be:

All considered it should be no more than 20 lines of code.

Update: make it 22...

#!/usr/bin/perl use strict; use warnings; open my $f,"<","x.txt"; my %tree; while (<$f>) { chomp; my ($c,$p)=split(/:/,$_); $tree{$c}=$p; } my %eert = reverse %tree; foreach my $l (keys %tree) { if (!defined($eert{$l})) { my @g=($l); while($l) { $l=$tree{$l}; push(@g,$l) if defined($l); } print join(':',@g),"\n"; } }

Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Replies are listed 'Best First'.
Re^2: Using Tree::Simple with while loop and input file.
by ikegami (Patriarch) on Jun 30, 2009 at 22:30 UTC
    You can cut the bottom loop by half (11 ⇒ 5) without making it more complex
    for (grep $tree{$_}, keys %tree) { my @g = $_; push @g, $_ while $_=$tree{$_}; print(join(':', @g), "\n"); }

    And why bother with an array?

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