in reply to Re^2: subroutine memory variable scope
in thread subroutine memory variable scope

"I made a few changes but I am still only getting the 1st tier of keys"

Then undo the changes, because the code I posted returns all levels of keys.

The key part which you stripped out is...

push @the_inputs, @{ analyse_user_inputs(...) };

When you understand what that's doing, then you'll understand why you shouldn't have stripped it out.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^4: subroutine memory variable scope
by austinj (Acolyte) on Dec 13, 2012 at 18:33 UTC
    The code you posted only works if the first tiers are all hashes - if they are arrays it won't work - which the example I posted has arrays - anyway I switched to checking against a hash since it should be faster processing. The following code works well regardless of how the reference comes in (HASH or ARRAY)
    sub get_user_keys { # subfunction traverses the a hash or array reference for all of the k +eys in all tiers my $hashORarray_ref = shift @_; my %the_inputs; if (ref $hashORarray_ref eq 'HASH') { foreach ( keys %{$hashORarray_ref} ){ # print "$_ \n"; unless(defined $the_inputs{$_}){$the_inputs{$_} = 1} @the_inputs{ keys %{get_user_keys($hashORarray_ref->{$_})} } = v +alues %{get_user_keys($hashORarray_ref->{$_})}; } } if (ref $hashORarray_ref eq 'ARRAY') { for (my $i = 0; $i < scalar(@{$hashORarray_ref}); $i++) { @the_inputs{ keys %{get_user_keys($hashORarray_ref->[$i])} } = v +alues %{get_user_keys($hashORarray_ref->[$i])}; } } return(\%the_inputs); }

      Well, I wasn't going to hand it to you on a plate! ;-)

      Personally I'd do it do it along these lines:

      sub analyse_user_inputs { my $r = shift; if (ref $r eq 'ARRAY') { return map { analyse_user_inputs($_) } grep ref, @$r } elsif (ref $r eq 'HASH') { return keys(%$r), map { analyse_user_inputs($_) } grep ref, va +lues(%$r) } else { return } }
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'