Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to find a way to make my code less redundant through the use of sub-routines. I need to pass different multi-dimensional hashes with unknown numbers of keys and quite often keys->keys->data. These hashes will be passed to the sub-routine one at a time, will need to be altered in some cases and just searched in others. Does anyone have any idea how I might go about this as I have been reading about PERLs way of making parameters for sub-routines into flat lists. Any help at all would be greatly appreciated. Thanx in advance, Val
  • Comment on Passing a multidimensional Hash to a sub-routine

Replies are listed 'Best First'.
Re: Passing a multidimensional Hash to a sub-routine
by Bird (Pilgrim) on Aug 16, 2002 at 15:48 UTC
    Pass them to your subs by reference.
    sub fishes { my $fishHoH = shift; for my $key (keys %{$fishHoH}) { print "$key fish: ", $fishHoH->{$key}->{fish}, "\n"; } } my %HoH = ( one => { fish => 'red' }, two => { fish => 'blue' } ); &fishes(\%HoH);

    -Bird

Re: Passing a multidimensional Hash to a sub-routine
by BUU (Prior) on Aug 16, 2002 at 15:39 UTC
    if your only passing one param you can just do  foo(%baz); sub foo{my %stuff=@_;} if you want to pass more then one and keep them seperate then use references like  foo(\%baz,\%qux); sub foo{my $stuff1_ref=shift; my $stuff2_ref=shift;}