Make a copy of structure within of a HoH?

U

usenet

Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?

Thanks!
 
I

it_says_BALLS_on_your forehead

Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?

$chart{'right'} = $chart{'left'};

instead of using Data Dumper to print this out, iterate through the
keys and print out the key/value pairs.
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?

$chart{'right'} = $chart{'left'};

instead of using Data Dumper to print this out, iterate through the
keys and print out the key/value pairs.

although i think a simpler data structure would be to make the value of
$chart{left} an anonymous hash reference:

$chart{left} = {
row => 1,
col => 1,
status => 'active',
type => 'regular',
};
 
L

Larry

it_says_BALLS_on_your forehead said:
Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?
$chart{'right'} = $chart{'left'};

instead of using Data Dumper to print this out, iterate through the
keys and print out the key/value pairs.

This does not seem like a good solution. It will not do a complete
copy of the structure... it will only copy references.

Instead:

use Storable qw(dclone);
 
P

Paul Lalli

Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?

The syntactic error can be repaired by assigning a scalar to a
reference, rather than assigning a scalar to a hash:
$chart{right} = $chart{left}

However, that will not do what you want (unless you want changes in
%{$chart{right}} to be reflected in %{$chart{left}} and vice versa).
A simple copying of references makes a shallow copy. You need to do a
deep copy.

perldoc -q copy
"How do I print out or copy a recursive data structure?"

Paul Lalli
 
I

it_says_BALLS_on_your forehead

Larry said:
it_says_BALLS_on_your forehead said:
Kindly consider this simplified script which illustrates my question
(and, no, I'm not REALLY using such poorly named keys - this is just
for illustration):

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;

my %chart;

$chart{'left'}{'row_1'}{'col_1'}{'status'} = 'active';
$chart{'left'}{'row_1'}{'col_1'}{'type'} = 'regular';

$chart{'right'} = %{$chart{'left'}}; # [ <<< Problem here! ]

print Dumper(\%chart);
__END__

As you can probably infer, my intent was to create another structure
($chart{'right'}) which duplicates what I have defined in
$chart{'left'}. But I'm not getting the syntax correct (and I've tried
a few variants without success).

Can someone tell me what the proper syntax would be?
$chart{'right'} = $chart{'left'};

instead of using Data Dumper to print this out, iterate through the
keys and print out the key/value pairs.

This does not seem like a good solution. It will not do a complete
copy of the structure... it will only copy references.

Instead:

use Storable qw(dclone);

ahh, you're right--i've been burned by the reference thing before. i
didn't read the OP's intent correctly, i was just going for the proper
syntax...
 
U

usenet

Can someone tell me what the proper syntax would be?

Thanks to all who replied in this thread. My intent was to make an
independent copy of the structure (not just a reference), so 'use
Storable qw/dclone/;' was the solution I was looking for (as explained
in 'perdoc -q copy'), ie:

$chart{'right'} = dclone( $chart{'left'} );

I appreciate the additional knowlege and insight I've gained here
today, especially regarding the distinction between 'deep' and
'shallow' copies, which I was unclear on before.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top