If you structure your data into a hash based tree then the following may help:

use strict; use warnings; use Data::Dump::Streamer; my $branch1 = { Leaves => [qw(Leaf1 Leaf2)], Branch2 => { Leaves => [qw(Leaf3)], Branch3 => { Leaves => [qw(Leaf4 Leaf5)], Branch4 => { Leaves => [qw(Leaf6)], } } }, Branch5 => { Leaves => [qw(Leaf7)], Branch6 => { Leaves => [qw(Leaf8 Leaf9)], } }, }; print "Original Tree:\n"; Dump ($branch1); removeBranch ($branch1); print "New Tree:\n"; Dump ($branch1); sub removeBranch { my ($root, $branch) = @_; my $leafKey; my $keep; $branch = $root unless $branch; for my $child (keys %$branch) { if ('ARRAY' eq ref $branch->{$child}) { $leafKey = $child; next; } if (removeBranch ($root, $branch->{$child})) { delete $branch->{$child}; } else { $keep = 1; } } return ! $keep unless $leafKey; my $leaves = $branch->{$leafKey}; return undef if @$leaves > 1; # Leave this branch alone push @{$root->{Leaves}}, @$leaves; delete $branch->{$leafKey}; return ! $keep; } __DATA__ Branch1 (Leaf1 Leaf2) Branch2 (Leaf3) Branch3 (Leaf4 Leaf5) Branch4 (Leaf6) Branch5 (Leaf7) Branch6 (Leaf8 Leaf9) Branch1 (Leaf1 Leaf2 Leaf3 Leaf6 Leaf7) Branch3 (Leaf4 Leaf5)

Prints:

Original Tree: $HASH1 = { Branch2 => { Branch3 => { Branch4 => { Leaves => [ 'Leaf6' +] }, Leaves => [ 'Leaf4', 'Leaf5' ] }, Leaves => [ 'Leaf3' ] }, Branch5 => { Branch6 => { Leaves => [ 'Leaf8', 'Leaf9' ] }, Leaves => [ 'Leaf7' ] }, Leaves => [ 'Leaf1', 'Leaf2' ] }; New Tree: $HASH1 = { Branch2 => { Branch3 => { Leaves => [ 'Leaf4', 'Leaf5' ] } }, Branch5 => { Branch6 => { Leaves => [ 'Leaf8', 'Leaf9' ] } }, Leaves => [ 'Leaf1', 'Leaf2', 'Leaf6', 'Leaf3', 'Leaf7' ] };

Perl is environmentally friendly - it saves trees

In reply to Re: Moving data in a tree by GrandFather
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.