in reply to Sub hash param by reference

If you are trying to pass a reference to some value in a hash to a sub, that is possible. That way the sub wouldn't know the key name. More usual would be to pass a ref to the whole hash and the sub does know the key name. Here is a demo of both possibilities. Hope that I'm not confusing the issue.
#!/usr/bin/perl use strict; use warnings; use Data::Dump qw(pp); $!=1; my %hash; $hash{id1} = 001; $hash{id2} = 002; print "initial hash: ", pp (\%hash), "\n"; my $hash_ref = \%hash; my $ref = \$hash{id2}; # A reference to id2 $$ref = 003; #use ref to a scalar (value of id2) print "modified hash: ", pp (\%hash), "\n"; modify_anon_hash_value ($ref); print "mod by sub : ", pp (\%hash), "\n"; more_normal_way($hash_ref); print "mod more usual: ", pp ($hash_ref), "\n"; sub modify_anon_hash_value #sub doesn't know which { #key's value that it is my $ref = shift; #modifying $$ref = 5; } sub more_normal_way #usual pass the entire hash as ref { my $href = shift; $href->{id2}=8; #sub know which key to use } __END__ initial hash: { id1 => 1, id2 => 2 } modified hash: { id1 => 1, id2 => 3 } mod by sub : { id1 => 1, id2 => 5 } mod more usual: { id1 => 1, id2 => 8 }

Replies are listed 'Best First'.
Re^2: Sub hash param by reference
by hankcoder (Scribe) on Jul 08, 2016 at 04:32 UTC

    Thank you Marshall, my $ref = \$hash{id2} is exactly what I am trying to look for.

    Your given example is perfect. I'm just curious, when calling sub modify_anon_hash_value ($ref) is it possible to do it like modify_anon_hash_value (\$hash{id2}) ? without need to do my $ref = \$hash{id2}.

    I have test run it with

    modify_anon_hash_value (\$hash{id2});
    The result return correctly without any error. Just to confirm if this way of coding is allowed or it may be a bad way to do it.

      Yes, you will be fine without creating an intermediate scalar. Using this method (modify_anon_hash_value (\$hash{id2});), I see right away in the callers code that a modification to $hash{id2} is what is intended.

      What is sent to the sub are aliases. It is possible to "hide" this reference creation in the sub. For fun, I coded the routine "confusing" below. The caller wouldn't expect that to happen, so I wouldn't do that.

      #!/usr/bin/perl use strict; use warnings; use Data::Dump qw(pp); $!=1; my %hash; $hash{id1} = 001; $hash{id2} = 002; print "initial hash: ", pp (\%hash), "\n"; my $hash_ref = \%hash; modify_anon_hash_value (\$hash{id2}); print "mod by sub : ", pp (\%hash), "\n"; confusing ($hash{id2}); print "mod by confusing : ", pp (\%hash), "\n"; sub modify_anon_hash_value #sub doesn't know which { #key's value that it is my $ref = shift; #modifying $$ref = 5; } sub confusing #a confusing mess { my $ref = \(shift); #same as $ref = \$_[0] $$ref=9; # @_ has aliases, not a copy } __END__ initial hash: { id1 => 1, id2 => 2 } mod by sub : { id1 => 1, id2 => 5 } mod by confusing : { id1 => 1, id2 => 9 }

        Thank you again Marshall. The confusing ($hash{id2}); is really confusing, esp my $ref = \(shift);. I will keep that in mind and avoid doing so.

Re^2: Sub hash param by reference
by hankcoder (Scribe) on Jul 10, 2016 at 07:52 UTC

    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;

    #... inside doSub1 my ($s) = $$hrec_ref{'id2'}; doSub2(\$s); $$hrec_ref{'id2'} = $s;
    I'm trying to see if there are any shortcut version of coding it.

      You need to use the sub keyword when declaring/defining subroutines. Here's a working version based on your code.

      use strict; use warnings; my (%hash); $hash{'id1'} = "1"; doSub1 (\%hash); # the return id2 should be changed within doSub2 when doSub1 call it. print "$hash{'id2'}\n"; sub doSub1 { # accept param as hash by reference my ($hrec_ref) = @_; $$hrec_ref{'id2'} = "0"; # doSub2(\$hrec_ref{'id2'}); Return Error. Not valid. (works for me - +Hippo) doSub2 (\$hrec_ref->{'id2'}); } sub doSub2 { # handle only a single string param by reference my ($href) = @_; $$href = "value changed in doSub2"; }

        Thank you hippo, that is the absolute solutions "doSub2 (\$hrec_ref->{'id2'})" I am looking for. And sorry I missed out the "sub" keyword on declaring subroutine.