[Purdue-pm] Copying Hashrefs - A Better Way?

David Jacoby jacoby at purdue.edu
Mon Mar 1 08:26:36 PST 2010


I want to copy a hashref. Well, I want to copy the hash reffed, not the 
pointer. This is not it.

     my $href_1 ;
     $href->{x} = 8 ;
     my $href_2 = $href_1 ;

You just get a pointer to the old hash, not a new hash. I want to copy 
the anonymous hash that $href_1 is referring to.

     my $href_1 ;
     my $href_2 ;
     $href->{x} = 8 ;

     %$href_2 =  map { $_ } %$href_1 ;

Yes, this works. But is there a better way?

-- 
Dave Jacoby                         Address: WSLR S049
Genomics Core Programmer            Mail:    jacoby at purdue.edu
Purdue University                   Phone:   765.49.67368


More information about the Purdue-pm mailing list