I have use for a system involving an (untied) hash which is used as a base for adding comments to a developing system. As the system is developed, a new hash is created, and I need to have my base hash updated to take in new entries, still retaining the comments previously added, but deleting entries which are made obsolete in the new hash. I can do a kind of update merge thus:
my %newbase = ( %oldhash, %newhash );
but this leaves the problem of keys or branches of the hash tree which were in hash1, but are not in hash 2, and should be eliminated from hash3 if it's to be the new base hash. My first draft solution relies on a recursive traversal of two hashes. This seems to work ( note: It has a few debugging stubs in it while I keep chewing it over). But is there MTOWTDI, and it there one simpler and/or less stack-intensive ? I'm using:
sub prunecheck { my ( $var1, $var2 ) = @_; my @var2keys = (); @var2keys = ( keys %$var2 ) if(ref($var2)=~ /HASH/); return unless defined $var1; for ( ref($var1) ) { if (/HASH/) { if ( !(@var2keys) ) { $$var1 = (); } else { for my $key1( keys %$var1 ) { if ( !exists( $$var2{$key1} ) ) { delete( $$var1{$key1} ); } else { # print "\t$key1\n"; prunecheck( $var1->{$key1}, $var2->{$key1} ); } } } } elsif (/ARRAY/) { foreach (@$var1) { prunecheck( $_, $var2 ) ; } } else { # print "\t\t$var1\n"; } } } prunecheck(\%newbase, \%newhash);
Appreciate comments, suggestions. Sneaking feeling I'm missing something obvious -- and simpler.

In reply to Updating one hash from another and pruning by quinkan

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.