in reply to Copying a hash to another hash problem

All the posts above are good, and to the point. I'll add my .02 gold as I didn't see a post which showed you how to go about doing exactly what you were doing, the way you were doing it.. (:P say that 10 times fast... No! not 'that' :) )
$frameno = "1"; $drawerno = "12"; $tempno = "44"; # assignment $framearray->{$frameno}{$drawerno} = $tempno; # reassignment %new_hash = %{$framearray}; # or only particular hashes thereof %some_frame = %{ $framearray->{$frameno} }; # Now if we loop through %some_frame our keys are # $drawerno and our values are the values assigned above while ( ($drawer,$val) = each(%some_frame) ) { print "$drawer -> $val\n"; } # would print # 12 -> 44
People have stated the deal with references so I'll leave that alone. I'm simply showing how to get the data the way you were trying.

It's my personal belief that even if I'm not necessarily doing it the "right way", I always like to be able to get it done. That way I know that at the very least I grasp the concept of whats going on. Then I'll find the module that does it appropriately. This way I learn, and implement cleanly, as opposed to getting stuck at some point, and not be able to proceed due to the fact that I may not have access to CPAN right now, and I need this done ASAP.

Cheers and happy hacking

/* And the Creator, against his better judgement, wrote man.c */

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

    I probably should have given more info to what I was attempting here.
    Basically, I have 700+ disks in a NAS environment, contained in 47 drawers within 7 frames. I want to monitor the temp of the drawers, so my array would contain (1-7) for the frames and (1-8) for the no of drawers within the frames (as they don't all contain the same no. of drawers).
    Giving as an example, (1,1) would be frame 1, drawer 1 would point to the value 30, or (5,8) would be frame 5, drawer 8 giving a value of 25.

    The reason I was copying the array, was that the next time the prog queried the temp, I could compare the arrays and notify me of any changes in the temp from the last run.

    Cheers

    Sean

      I realise it is 17 years later now, but for what it is worth now in 2019, you don't have to use a copy for this, only return a new reference / object in the acquisition function that queriers the data of your hard disks. So, don't use global hashes/arrays with this function. Then, two successive calls of the acquisition function will already return two separate instances of your data and so there is no need to copy this any further.