dclone and copying one element of a hash

T

TonyShirt

I've been using dclone to copy a hash to another hash. At one point
in my program I need to copy one element of a hash into another.

Heres the code

my %a = { 1 => {a1=> 1, a2 =>2, a3=>3
2 => {a1=> 4, a2 =>5, a3=>6};

my %b = %{ dclone(\%a) }

#now I want to clear %b and copy a element from a% into %b

my %b=();

$b{1} = dclone( $a{1}); # This dosent seem to work!

Any suggestions?
 
A

A. Sinan Unur

(e-mail address removed) (TonyShirt) wrote in
I've been using dclone to copy a hash to another hash. At one point
in my program I need to copy one element of a hash into another.

Heres the code

my %a = { 1 => {a1=> 1, a2 =>2, a3=>3
2 => {a1=> 4, a2 =>5, a3=>6};

my %b = %{ dclone(\%a) }

#now I want to clear %b and copy a element from a% into %b

my %b=();

$b{1} = dclone( $a{1}); # This dosent seem to work!

Any suggestions?

My first suggestion is not to re-type code into the newsreader. The code
you have above does not even come close to compiling without errors. So
nothing there works, it is hard to tell what the actual problem is versus
just your mis-mash.

How about the following?

use Data::Dumper;
use Storable qw( dclone );

my $a = {
1 => {
a1 => 1,
a2 => 2,
a3 => 3,
},
2 => {
a1 => 4,
a2 => 5,
a3 => 6,
},
};

my $b = dclone( $a );

print Dumper $b;

$b = { };
$b->{1} = dclone( $a->{1} );

print Dumper $b;
__END__
 
T

TonyShirt

A. Sinan Unur said:
(e-mail address removed) (TonyShirt) wrote in


My first suggestion is not to re-type code into the newsreader. The code
you have above does not even come close to compiling without errors. So
nothing there works, it is hard to tell what the actual problem is versus
just your mis-mash.

How about the following?

use Data::Dumper;
use Storable qw( dclone );

my $a = {
1 => {
a1 => 1,
a2 => 2,
a3 => 3,
},
2 => {
a1 => 4,
a2 => 5,
a3 => 6,
},
};

my $b = dclone( $a );

print Dumper $b;

$b = { };
$b->{1} = dclone( $a->{1} );

print Dumper $b;
__END__

Sorry -- I was just trying to simplify the analysis. Next time I'll
provide all the code. Thanks for the help it worked!

I have another question. What is the diffence between Dumper $b and
Dumper %$b? Why do they give diffrent results?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top