in reply to Re: Sub hash param by reference
in thread Sub hash param by reference
Hi Marshall, sorry I have confused your previous answer with my initial questions. Your previous answer is helpful. Just my initial case is little bit different. When inside a sub, the value pass in is already a hash reference and I would like to call another sub but only in refer to specific rec of that hash. Please have a look of the codes below;
my (%hash); $hash{'id1'} = "1"; doSub1(\%hash); # the return id2 should be changed within doSub2 when doSub1 call it. print "$hash{'id2'}\n"; doSub1 { # accept param as hash by reference my ($hrec_ref) = @_; $$hrec_ref{'id2'} = "0"; # Going to call doSub2 by passing Only $$hrec_ref{'id2'} as reference. # Because $hrec_ref is a hash reference already, How I can call it? # doSub2(\$hrec_ref{'id2'}); Return Error. Not valid. # doSub2($hrec_ref{'id2'}); Also return Error. } doSub2 { # handle only a single string param by reference my ($href) = @_; $$href = "value changed in doSub2"; }
My problem confusing now is IF the hash is in a reference value and I need call another sub to process only specific hash rec. My current only way to deal with this is;
I'm trying to see if there are any shortcut version of coding it.#... inside doSub1 my ($s) = $$hrec_ref{'id2'}; doSub2(\$s); $$hrec_ref{'id2'} = $s;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Sub hash param by reference
by hippo (Archbishop) on Jul 10, 2016 at 10:26 UTC | |
by hankcoder (Scribe) on Jul 10, 2016 at 12:44 UTC |