Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Copying a hash to another hash problem

by mfriedman (Monk)
on Oct 22, 2002 at 14:29 UTC ( #207090=note: print w/replies, xml ) Need Help??

in reply to Copying a hash to another hash problem

First a question: Your data appears to be entirely numeric. Why are you using hashes instead of arrays?

Secondly, your %newhash should not be empty after the copy operation, but be advised that it will contain references to the ORIGINAL second level hashes from the first assignment. Example:

use strict; use Data::Dumper; my %first; $first{'foo'}{'bar'} = 1; $first{'foo'}{'baz'} = 2; $first{'bah'}{'oof'} = 3; print "First hash:\n"; print Dumper \%first; my %second = %first; print "\nSecond hash\n"; print Dumper \%second; print "\nFirst nested hash address: $first{'foo'}\n"; print "Second nexted hash address: $second{'foo'}\n";


First hash: $VAR1 = { 'bah' => { 'oof' => 3 }, 'foo' => { 'baz' => 2, 'bar' => 1 } }; Second hash $VAR1 = { 'bah' => { 'oof' => 3 }, 'foo' => { 'baz' => 2, 'bar' => 1 } }; First nested hash address: HASH(0x804ee40) Second nexted hash address: HASH(0x804ee40)

Replies are listed 'Best First'.
Re: Re: Copying a hash to another hash problem
by Anonymous Monk on Oct 22, 2002 at 14:42 UTC

    Thankyou all for your constructive replies. I can see my obvious mistake that the copy of one hash to another is fundamentally flawed in my code and has been pointed out, i'm using numeric values for both keys and values, so I will rewrite the code using arrays.

    Again, thanks for the advice.



      i'm using numeric values for both keys and values

      Well, this isnt intrinsically a bad thing. But more often than not it is.

      The reason I say this is because if your indexes are sparse, ie 1, 500,623,7439,104029 or the like, then a hash is a much more suitable container type than an array is. (A hash would store the above usin 5 slots in a hash, an array would store at minimum 104029 elements.) However if the array is dense, 1..9,11..19,21..29 etc then an array is a more suitable approach. (Each slot in a hash is bigger than in an array, so even with the 4 wasted slots in the array would still be much smaller than the 26 allocated slots in the hash.)

      Also, the difference isnt so much due to what you store in the values, but what you store in the keys.

      Good luck and dont forget strict. :-)

      --- demerphq
      my friends call me, usually because I'm late....

        Maybe I should have given an explanation of my requirements.

        Basically, I have a NAS setup with 700+ disks contained within 45 drawers split over 7 frames that I want to watch the temp on.
        The code queries the drawers one by one and maps which drawers are in which frames and collects the current temp(also producing a pictorial map of the system).

        So the array would only ever contain a max of (7) for the frames and (1-8) for the drawers within the frames (as not all frames have the same no. of drawers).

        The copying (or snapshot) of the array was for comparing against the next time the prog went to check the temp to inform me of differences.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-06-13 16:53 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (57 votes). Check out past polls.