in reply to RE: Hash Push
in thread Hash Push

You're not recursively filling a recursive data structure though, so the routine should not be passing data to merge: it should either be accessing a single reference and updating in place, or accessing a global (gack!).

For example:

my %global_data; fill_recursively(\%global_data, 10); sub fill_recursively { my $data_href = shift; # hashref to modify my $count = shift; # what level are we push @{$data_href->{"key $_"}}, "level $count" for 1..$count; fill_recursively($data_href, $count - 1) if $count > 1; } use Data::Dumper; print Dumper(\%global_data);
The push is accessing the global data directly. Put whatever you want in there. Does that help?

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: RE: RE: Hash Push
by Adam (Vicar) on Aug 16, 2000 at 22:21 UTC
    Hey, I like that... Could I hide the whole thing too?
    Maybe write:
    sub GetData { my %data; sub Recursive { # do stuff # push onto %data # call Recursive if appropriate } Recursive(); return \%data; } my %data = %{GetData()}; # Can this be cleaner?
      Well, I'm not sure why you'd want to do that. For one, you now have nested subroutines with the ol' "Value won't stay shared" in the middle to boot. If you control the call to your subroutine, you can require they pass it by reference as the first parameter. If you don't, wrapper it up in a helper routine:
      sub routine_that_they_call { my @args = @_; my %data_to_be_filled; recursive_subroutine(\%data_to_be_filled, @args); return \%data_to_be_filled; }
      Now you don't have to define a nested subroutine.

      -- Randal L. Schwartz, Perl hacker