ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
My code is sending a hash by reference to a function and adds an element to it. The problem is that the new element isn't in the hash that I sent.
my %hash = ( 'animal' => 'bear', 'plant' => 'oak', 'fungus' => 'truffle', ); test(\%hash); print join(",", keys (%hash)),"\n"; sub test { my ($ref) = @_; my %ref_hash = %{$ref}; $ref_hash{'new'} = 'new_v'; print join(",", keys (%ref_hash)),"\n"; }
Output:
plant,new,fungus,animal + + plant,animal,fungus
I want the 'new' element to be also in the new one. I think I didn't quit understand how references work.
Output I want:
plant,new,fungus,animal + + plant,new,fungus,animal
2018-03-22 Athanasius Added code and paragraph tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: passing hash ref to a function
by hippo (Archbishop) on Mar 16, 2018 at 17:08 UTC | |
|
Re: passing hash ref to a function
by ovedpo15 (Pilgrim) on Mar 16, 2018 at 19:29 UTC | |
by poj (Abbot) on Mar 16, 2018 at 19:50 UTC | |
by alexander_lunev (Pilgrim) on Mar 16, 2018 at 20:11 UTC | |
by haukex (Archbishop) on Mar 17, 2018 at 10:17 UTC | |
by Anonymous Monk on Mar 17, 2018 at 03:25 UTC |