Philippe has asked for the wisdom of the Perl Monks concerning the following question:
Hi!
I've been unsucessfully trying to modify a hash via its reference: it have only found a lame workaround. Can anyone enlighten me?
Thanks!
Philippe
PS: here's a reduced testcase
#!/bin/perl -W # use strict; # my ( %Fruits ) = ( "Apples", 3, "Oranges", 6 ); &Print_Hash; # &Modify_Hash_1 ( \%Fruits ); &Print_Hash; # This doesn't seem to have any effect... # &Modify_Hash_2 ( \%Fruits ); &Print_Hash; # But this does... # sub Print_Hash { my ( $Fruit ); foreach $Fruit ( sort ( keys ( %Fruits ) ) ) { printf "\n%-7s: %d", $Fruit, $Fruits{$Fruit}; }; print "\n\n"; }; # sub Modify_Hash_1 { my ( $Ref_to_Hash ) = @_; $Ref_to_Hash = { "Pears", 5, "Peaches", 7 }; }; # sub Modify_Hash_2 { my ( $Ref_to_Hash ) = @_; my ( $Fruit ); foreach $Fruit ( keys ( %$Ref_to_Hash ) ) { delete ( $$Ref_to_Hash{$Fruit} ); }; $$Ref_to_Hash{"Pears"} = 5; $$Ref_to_Hash{"Peaches"} = 7; }; # # __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Modify a hash via its reference
by roboticus (Chancellor) on Jan 23, 2008 at 11:50 UTC | |
by bart (Canon) on Jan 23, 2008 at 12:36 UTC | |
|
Re: Modify a hash via its reference
by moritz (Cardinal) on Jan 23, 2008 at 11:57 UTC | |
|
Re: Modify a hash via its reference
by RMGir (Prior) on Jan 23, 2008 at 12:25 UTC | |
|
Re: Modify a hash via its reference
by bradcathey (Prior) on Jan 23, 2008 at 12:54 UTC | |
by Errto (Vicar) on Jan 23, 2008 at 22:51 UTC | |
by bradcathey (Prior) on Jan 24, 2008 at 03:26 UTC | |
|
Re: Modify a hash via its reference
by Philippe (Novice) on Jan 23, 2008 at 13:54 UTC |