in reply to Modify hash reference itself

This won't work that way. You can only change the reference to point to another hash, but you cannot replace the hash itself. You could do this:
$myhash = \%myhash; mergetwohashes($myhash, \%b); sub mergetwohashes { #my($a)=$_[0]; my($b)=$_[1]; $_[0] = { %{$_[0]}, %{$b} }; }
But this will require you to only pass hash references, and work on the changed reference later. It will only change $myhsash, NOT %myhash.

Or you could do this:

sub mergetwohashes { my($a)=$_[0]; my($b)=$_[1]; %{$a} = ( %{$a}, %{$b} ); }
But this way you only change the (complete) contents of the hash, the hash itself stays the same. But I'd guess this is just what you want...

Search, Ask, Know

Replies are listed 'Best First'.
Re: Re: Modify hash reference itself
by jweed (Chaplain) on Dec 02, 2003 at 00:38 UTC
    Just to help clarify Beechbone's answer, $a = {%$a, %$b); changes to which hash $a refers, while %$a = {%$a, %$b); changes the hash to which $a refers. English isn't especially precise in this regard either. :) But I hope this helps.

    P.S. - You don't need braces to deref bare scalars - %$a works fine.


    Who is Kayser Söze?
Re: Re: Modify hash reference itself
by Anonymous Monk on Dec 02, 2003 at 00:41 UTC
    Your second example worked perfectly, thank you. You know, in my struggle I almost tried that, I had:

    %{$a} = { %{$a}, %{$b} };

    I've been writing perl for 6 years, and still I get tripped up. Thanks again Beechbone