473,378 Members | 1,156 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,378 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 6373
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.