Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: hash question

by AnomalousMonk (Archbishop)
on May 26, 2016 at 15:48 UTC ( [id://1164206]=note: print w/replies, xml ) Need Help??


in reply to hash question

As an example of what BrowserUk and GrandFather are talking about, print the addresses of the hash references you're passing around:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $hash = {}; print 'Ma: ', $hash; ;; do_something($hash); ;; print 'Mb: ', $hash; print Dumper($hash); ;; sub do_something { my $hash = shift; print 'Sa: ', $hash; ;; $hash = { a => 'alpha', b => 'beta', }; print 'Sb: ', $hash; print Dumper( $hash ); } " Ma: HASH(0x9cd06c) Sa: HASH(0x9cd06c) Sb: HASH(0x9cd16c) $VAR1 = { 'a' => 'alpha', 'b' => 'beta' }; Mb: HASH(0x9cd06c) $VAR1 = {};
Note that the address of the  $hash reference variable in the subroutine changes between point Sa and point Sb when you assign another anonymous hash reference to it, but the  $hash reference variable in the "main" part of the code never changes its address (or its contents).

Replace the
    $hash = { a => 'alpha',  b => 'beta', };
statement in the subroutine with
    $hash->{a} = 'alpha';
    $hash->{b} = 'beta';
statements that assign by reference to the referent and see what happens to all the hash reference addresses: they never change.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: hash question
by duyet (Friar) on May 27, 2016 at 05:55 UTC
    Thanks all for your prompt responses. I don't understand why the address has been changed between "Sa:" and "Sb:" by assigning another anonymous hash. I expected "Sb:" would be the same as "Sa:", since i was using the same var. I changed the code a bit:
    sub do_something { my $h = shift; print '1. do_something(): ' . $h . "\n"; %{ $h } = ( a => 'alpha', b => 'beta' ); print '2. do_something(): ' . $h . "\n"; print Dumper( $h ); }
    and it shows:
    hash = HASH(0x490d30) 1. do_something(): HASH(0x490d30) 2. do_something(): HASH(0x490d30) { 'a' => 'alpha', 'b' => 'beta' } { 'a' => 'alpha', 'b' => 'beta' }
    The address is not changed ... the behaviour is not consistent to me :)

      You are missing one slight catch. When you do

      my $hash = shift;
      you get pointer to the object passed to the function. When you do
      $hash = {a => "alpha"};
      you create new object and store its pointer in the variable that previously contained pointer to another object. Finally, when you do
      %{ $hash } = ( a => 'alpha', b => 'beta' );
      then you take object pointed by the variable and store in that object new set of keys.

      You have to distinguish "objects" and "pointers to objects", the latter are called "references" in perl, then things may become more consistent for you.

      You are printing the content of the variable. It's two different variables. If you print the variables directly instead of via Dumper,
      my $hash = {}; do_something( $hash ); print Dumper( $hash ); sub do_something { my $hash = shift; $hash = { a => 'alpha', b => 'beta', }; print Dumper( $hash ); }
      you'll get two different addresses:
      HASH(0x3c4a4)HASH(0x3c39c)
      Update: actually, this is also only the contents. Best answer so far to your OP is this by AnomalousMonk
      I don't understand why the address has been changed between "Sa:" and "Sb:" by assigning another anonymous hash. I expected "Sb:" would be the same as "Sa:", since i was using the same var. [Emphasis added]

      That's like saying

      In the code
          $x = 4;
          $x = 7;
      I don't understand why the value of  $x is 7 after the second assignment to $x: I'm using the same variable.

      As others have noted, it's vital to distinguish between a variable, the content of the variable, and, if the content of the variable is a reference, the referent of the reference. References are tricky.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found