473,225 Members | 1,266 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Problem Copying Hash of Arrays (REUSED_ADDRESS)

Hello,

Consider this
Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use Data::Dumper;
  4.  
  5. my %hash1=(a=>[1,1],
  6.         b=>[2,2],
  7.         c=>[3,3],);
  8.  
  9. my %hash2=%hash1;
  10.  
  11. $hash1{a}[0]=10;
  12.  
  13. print Dumper(%hash2);
  14.  
The problem is that when copying %hash1 into %hash2, the values of %hash2 are just a references to
the SAME arrays in %hash1, so changing an array element in %hash1 changes the respective element
in the new %hash2. But I want to store the old value of %hash1 into %hash2.
In a first attempt to solve that problem, I come with that :
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. use strict;
  3. my %hash1=(a=>[1,1],
  4.         b=>[2,2],
  5.         c=>[3,3],);
  6.  
  7. my %hash2=();
  8. while ( my ($key,$val)= each %hash1) {
  9.   my @arr=@{$val};
  10.   $hash2{$key}=\@arr;
  11. }
  12.  
  13. $hash1{a}[0]=10;
  14. print Dumper(%hash2);
  15.  
but it's quite ugly.
I've also found that can surround the problem by assigning not a new value to the array (see: $hash1{a}[0]=10; )
but assigning a whole new array like this : $hash1{a}=[10,10];

But I still can't understand why the arrays are "copied" with the same address into %hash2 and
how I can store a %HoA into another %Hoa ...

please help
Feb 5 '07 #1
3 6353
miller
1,089 Expert 1GB
This is basically functioning as it shoud. If you want complete clones of each of the data structures inside the %hash, then you'll have to copy each of them. The easiest way to accomplish this would be to make a subroutine that recursively operated on arrays and unreferenced each element. The only function that it's important to be familiar with to accomplish this is ref.

http://perldoc.perl.org/functions/ref.html

Here is the code and function that I quickly through together from your example.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Data::Dumper;
  3.  
  4. my %hash = (
  5.     a    => [1,1],
  6.     b    => [2,2],
  7.     c    => [3,3],
  8. );
  9.  
  10. my %copy = %hash;
  11. my %clone = clone(%hash);
  12.  
  13. $hash{a}[0]=10;
  14.  
  15. print 'copy - ' . Dumper(\%copy);
  16. print 'clone - ' . Dumper(\%clone);
  17.  
  18.  
  19. sub clone {
  20.     map { ! ref() ? $_ : ref eq 'HASH' ? {clone(%$_)} : ref eq 'ARRAY' ? [clone(@$_)] : die "$_ not supported" } @_;
  21. }
  22.  
Also, just as a stylistic note, I find it much better if I only pass scalars to Dumper. In this instance that means passing a reference to the hashes you want to explore instead of the hashes themselves. You'll find the output to be much cleaner in my opinion.

Good luck.
Feb 5 '07 #2
KevinADC
4,059 Expert 2GB
another possibility:

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. use Storable qw/dclone/;
  3. my %hash1=(a=>[1,1],
  4.         b=>[2,2],
  5.         c=>[3,3],);
  6.  
  7. my $hash2 = dclone(\%hash1);
  8.  
  9. $hash1{a}[0]=10;
  10.  
  11. print Dumper(\$hash2,\%hash1);
$hash2 will be a copy only of %hash1, but the structure of $hash2 is a little different in that $hash2 is a reference to a hash of arrays instead of a hash of arrays like %hash1 is.
Feb 5 '07 #3
Really appreciate your help... Thanks
Feb 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
2
by: shan_rish | last post by:
Hi CLCers, In the below program the error message while compiling is /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue...
3
by: railrulez | last post by:
Hi, Attached is a program which uses a hash_set, but I cant seem to get find() or iterators working on it. I'm not sure whether hash_set is std C++, but I dont know where else to ask. ...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
0
by: berwiki | last post by:
I am trying to copy a table to another SQL 2000 Database, but I continually get errors. When I right-click, choose All-Tasks, Export-Data and go through the DTS settings, I get an 'Unspecified...
3
by: ipointer | last post by:
I am an experienced programmer that is fairly new to C++. I have written a small installation executable that is supposed to copy a set of files from within a directory just on the CD. The problem...
1
by: =?Utf-8?B?Y3JhbmtlX2JveQ==?= | last post by:
Hi Folks, I'm not sure where this post belongs since I'm using managed vc.net, but the issue is around GDI BitBlt. Here is a summary of the problem: - I am trying to copy a bitmap of my main...
1
by: =?Utf-8?B?c3BhaW5jYw==?= | last post by:
I have always been able to create folders with pictures from my digital camera. Then by selecting all pictures and then selecting copy to cd, I could copy my pictures to a CDRW. Now I am...
3
by: itsmenarwal | last post by:
Hi I want that nobody can see my password even in database.. So i used hash function like this $passowrd_hash=hash('shal',$_POST); NOw easily i can store this password_hash value into...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.