You didn't specify anything about your data structure, so I made a guess at it.
use strict; use warnings; BEGIN { package Node; sub new { my ($class, $name) = @_; return bless({ name => $name, }, $class); } sub name { $_[0]{name} } } BEGIN { package Label; sub new { my ($class, $name, $nodes, $branches) = @_; return bless({ name => $name, nodes => $nodes, branches => $branches, }, $class); } sub name { $_[0]{name} } sub nodes { @{ $_[0]{nodes} } } sub flatten { my ($self) = @_; my @orphans; local *_flatten = sub { my ($self) = @_; our @nodes; local *nodes = $self->{nodes}; our @branches; local *branches = $self->{branches}; push @orphans, shift @nodes if @nodes == 1; @branches = map _flatten($_), @branches; return @nodes ? $self : @branches; }; _flatten($self); push @{ $self->{nodes} }, @orphans; } sub visit { my ($self, $visitor) = @_; local *_visit = sub { my ($self, $depth) = @_; our @branches; local *branches = $self->{branches}; $visitor->($self, $depth); for my $branch ( @branches ) { _visit($branch, $depth+1); } }; _visit($self, 0); } } { sub node { return Node->new(@_); } sub label { return Label->new(@_); } sub printer { my ($label, $depth) = @_; my $indent = ' ' x $depth; print("$indent+ ", $label->name(), "\n"); for my $node ( $label->nodes() ) { print("$indent = ", $node->name(), "\n"); } } my $tree = ( label('Label1', [ node('Node1'), node('Node2') ], [ label('Label2', [ node('Node3') ], [ label('Label3', [ node('Node4'), node('Node5') ], [ label('Label4', [ node('Node6') ], [ ]) ]) ]) ]) ); $tree->visit(\&printer); print("\n"); $tree->flatten(); $tree->visit(\&printer); print("\n"); }
+ Label1 = Node1 = Node2 + Label2 = Node3 + Label3 = Node4 = Node5 + Label4 = Node6 + Label1 = Node1 = Node2 = Node3 = Node6 + Label3 = Node4 = Node5

In reply to Re: Moving data in a tree by ikegami
in thread Moving data in a tree by hoffmann

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.