http://qs321.pair.com?node_id=33575

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

I've got several hash of hashes, i.e. :
$hash{name} = { date => "09-22-00", city=>"Chicago",};
I want to pass this to a function like this:
function (\%hash); function (\%hash1); function (\%hash2); sub function { # now I want to use the reference to access the hash I just # passed, but I'm not sure how. I need to add items to the # hash of hashes as well. # this will flatten the hash, I'd prefer to just know the # name of the hash I just passed %hash_reference = @_; for $key (keys %hashreference){ $hashreference{$key} ={moredata=>"datadatadata"}; } }

Replies are listed 'Best First'.
Re: how do I use a hash of hashes reference to a function ?
by Fastolfe (Vicar) on Sep 22, 2000 at 02:28 UTC
    You may wish to wrap your code in <code> tags next time. I'm not 100% sure what you're asking, but perhaps this code example will answer your question:
    sub function { my $hashref = shift; $hashref->{three} = 3; } my %hash = ( one => 1, two => 2 ); &function(\%hash); print join(", ", keys %hash); # one, two, three
    You can also have the same behavior like this:
    sub function (%) { my %hashref = shift; ... } &function(%hash); # passes %hash as a ref
    Then there's this:
    sub function { my %hash = %{shift}; # de-references $hash{three} = 3; # Does not affect the real %hash! }
      Sorry, I neglected to read your title very well and did not take this one step further:
      %hash = ( one => { a => 10, b => 20 }, two => { c => 30, d => 40 } ); &function(\%hash); sub function { my $hash = shift; $hash->{two}->{d} = 50; # changes 'd' }
      You can go as deep as you want here with hashes of hashes of hashes. Hope that helps...
Re: how do I use a hash of hashes reference to a function ?
by arturo (Vicar) on Sep 22, 2000 at 18:33 UTC
    Alternately, although this is less readable, you can dereference a hash in the subroutine with:
    foo(\%hash); sub foo { my %hash = %{shift()}; # rest of function here, which manipulates hash directly }

    Call me perverse, but I just love the way that line looks (from what I understant, the () is required when you put shift in the braces to force perl to recognize it as a function call and not a hash key ... but I've been wrong about these things in the past ...

      This does not affect the real %hash. By de-referencing it like that, you're essentially creating a copy. The item on the right-hand-side is now a real hash (by virtue of wrapping it in %{...}), not a reference. Thus, you are effectively doing this:
      %hash1 = %hash2;
      This does not create an alias, but a copy. Changes to %hash1 have no effect on %hash2.

      And no, you don't have to put parens around your split call. Perl does the correct thing here, but it may be more readable to others if you write it with parens anyway. Here is some test-case code you can try yourself:

      sub test { my $hash1 = shift; my %hash2 = %{shift}; $hash1->{three} = 3; $hash2{three} = 3; } my %a = (one => 1, two => 2); my %b = (one => 1, two => 2); &test(\%a, \%b); print join(", ", keys %a), "\n"; print join(", ", keys %b), "\n"; # three, two, one # two, one