in reply to Variable scoping when a sub calls itself??
Don't do that. Don't mix $hashvar with $hashref.
Also don't call subs with a leading '&', it might have unexpected behaviour.
Update: Here is a small script that does what you want, by constructing a new copy of the data structure instead of deleting items:
use strict; use warnings; use Data::Dumper; use Scalar::Util qw(reftype); my $hashvar = { emp => {}, hemp => { splay => {}, hay => { go => 1, ho => {}, }, tay => [], may => [ 'way' ], }, }; sub clean { my $ref = shift; return $ref unless ref $ref; if (reftype($ref) eq 'HASH'){ my %result; for (keys %$ref){ my $tmp = clean($ref->{$_}); $result{$_} = $tmp if (defined $tmp); } if (keys %result){ return \%result; } else { return; } } elsif (reftype($ref) eq 'ARRAY'){ return @$ref ? $ref : undef; } } print Dumper($hashvar, clean($hashvar)); __END__ $VAR1 = { 'hemp' => { 'may' => [ 'way' ], 'tay' => [], 'hay' => { 'ho' => {}, 'go' => 1 }, 'splay' => {} }, 'emp' => {} }; $VAR2 = { 'hemp' => { 'may' => $VAR1->{'hemp'}{'may'}, 'hay' => { 'go' => 1 } } };
I'm too tired to tell if it really works, so check it yourself ;-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Variable scoping when a sub calls itself??
by cosmicperl (Chaplain) on Apr 15, 2008 at 22:50 UTC | |
by ikegami (Patriarch) on Apr 15, 2008 at 23:17 UTC |