Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Addition of hashes

by the_dark_lord (Novice)
on Feb 03, 2017 at 06:32 UTC ( [id://1180925]=perlquestion: print w/replies, xml ) Need Help??

the_dark_lord has asked for the wisdom of the Perl Monks concerning the following question:

Hi I was writing a program and instead of adding values in 2 different hashes, I summed 2 hashes themselves.

I caught my mistake but the value printed by the incorrect code made me curious.

Basically, if I add 2 hashrefs, I get some value which depends only on the first hashref added.

Example: If I try to run the code

my $a = { # 'key1' => 'valdsgdjgf`', # 'key2' => 'val2', }; my $b = { # 'key3' => 'val3', # 'key4' => 'val4', }; my $sum = $a + $b; print "$sum\n";

I get some random value: 15401280

If I make a change to hashref $a, then this value changes. But if I make a change to hashref $b, then this value does not change.

Does anybody know what this value is?

Replies are listed 'Best First'.
Re: Addition of hashes
by NetWallah (Canon) on Feb 03, 2017 at 07:25 UTC
    You are adding memory addresses.

    If you change what $b points to, the add it to $a, the $sum will change.

    You state : I make a change to hashref $b

    Did you change the internal content of the hashref, or did you assign a new hashref to $b ? (Thereby changing the memory address)

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re: Addition of hashes
by vinoth.ree (Monsignor) on Feb 03, 2017 at 07:19 UTC

    Using a reference as a number produces an integer representing its storage location in memory. The only useful thing to be done with this is to compare two references numerically to see whether they refer to the same location


    All is well. I learn by answering your questions...
      To expand a bit:

      The value of a hashref depends on the context - and not only scalar vs list, but it's different between numeric and string context, e.g.

      perl -e 'print {}' # string-context HASH(0x114dcb8) perl -e 'print 0 + {}' # numeric context 18996408 perl -e '{} + 0' # does not compile syntax error at -e line 1, near "} +"
Re: Addition of hashes
by BillKSmith (Monsignor) on Feb 03, 2017 at 14:14 UTC
    Did you intend to 'merge' the hashes?
    use strict; use warnings; use Data::Dumper; my $a = { 'key1' => 'valdsgdjgf`', 'key2' => 'val2', }; my $b = { 'key3' => 'val3', 'key4' => 'val4', }; my $c = {%$a, %$b}; print Dumper($c); OUTPUT: $VAR1 = { 'key2' => 'val2', 'key4' => 'val4', 'key3' => 'val3', 'key1' => 'valdsgdjgf`' };
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1180925]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-29 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found