quinkan has asked for the wisdom of the Perl Monks concerning the following question:
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:my %newbase = ( %oldhash, %newhash );
Appreciate comments, suggestions. Sneaking feeling I'm missing something obvious -- and simpler.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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Updating one hash from another and pruning
by Abigail-II (Bishop) on Aug 13, 2002 at 15:04 UTC | |
by Anonymous Monk on Aug 15, 2002 at 02:21 UTC |