Hi Monks

I have a data structures made up of HOHOH etc which represents a tree. I am trying to find a way of removing an entire 'layer' of the tree while keeping the other layers intact and still linked to each other. As an example I may start with

$W={ 'A' => { 'L' => { 'D'=>99, 'X'=>99, }, 'C' =>{ 'E'=>99 }, }, 'X' => { 'Q'=>{ 'O'=>99, }, 'T'=>{ 'U'=>99, }, }, };
and I want to remove all of 'level' 2 ( ie L,C,Q,T ) leaving me with;
$X={ 'A' => { 'D'=>99, 'X'=>99, 'E'=>99 }, 'X' => { 'O'=>99, 'U'=>99, }, };
Doesn't sound too bad ? The only complication is the tree can have any number of layers so I've been leaning towards various recursive ideas and using dclone but I'm not getting anywhere fast. I'm also thinking that it's probably easier to create a new hash instead of changing the source, unlike my lame effort below.
printTree($W); # just dumps HOHOH - could use data::dumper - remove +d remove1($W,2); # ie remove layer 2 ( counting from 1 ) print "--------------------\n"; printTree($W); sub remove1 { my($refToHash)=shift; my($stage)=shift; foreach $a (keys %{$refToHash}) { if($depth==$stage-1) # then we've hit the level before the lev +el to be removed/ { print "removing $a\n"; foreach $b (keys %{$refToHash->{$a}}) { $refToHash->{$b} = $refToHash->{$a}->{$b}; delete($refToHash->{$a}); } } else { $depth++; remove1($refToHash->{$a},$stage); $depth--; } } }
output is;
perl delete_mid2.pl A C E L D X X Q O T U removing C removing L removing Q removing T -------------------- A D E X X O U
so sort of ok but I'm really not happy with it. Removing layer 1 is also ok but layer 3 does not work. Can anyone suggest a better way of doing this. I've been staring at this (from Randal L. Schwartz, Perl hacker) for the last 3 hours and trying to think of a way of making it layer sensitive ...
sub deep_copy { my $this = shift; if (not ref $this) { return $this; } elsif(ref $this eq "HASH") { return {map { $_ => deep_copy($this->{$_}) } keys %$this}; } }
I would really appreciate ANY suggestions. Thanks in advance. Jeff

In reply to recursive hash layer removal question by wertert

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.