in reply to Modify a hash via its reference

Philippe:

The problem is that your Modify_Hash_1 routine is accepting the hash ref, and then throwing the reference away by assigning it a new value. By modifying your Modify_Hash_1 routine to the following, you can change the values in %Fruits.

sub Modify_Hash_1 { my ( $Ref_to_Hash ) = @_; $Ref_to_Hash->{Apples}=5; # This line would discard the reference to %Fruits, and create a *new* + # hash with the specified values # $Ref_to_Hash = { "Pears", 5, "Peaches", 7 }; };
...roboticus

Update: s/discards/discard/, s/taking/accepting/, added code tags in explanatory text.

Replies are listed 'Best First'.
Re^2: Modify a hash via its reference
by bart (Canon) on Jan 23, 2008 at 12:36 UTC
    Good work.
    # This line would discard the reference to %Fruits, and create a *new* # hash with the specified values # $Ref_to_Hash = { "Pears", 5, "Peaches", 7 };
    The solution is to not change the reference, but the dereferenced hash:
    %$Ref_to_Hash = ( "Pears", 5, "Peaches", 7 );