Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Make a copy of structure within of a HoH?

Reply
Thread Tools

Make a copy of structure within of a HoH?

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      01-10-2006
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:umper; $Data:umper::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!

--
http://DavidFilmer.com

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-10-2006

use...@DavidFilmer.com wrote:
> 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:umper; $Data:umper::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.

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-10-2006

it_says_BALLS_on_your forehead wrote:
> use...@DavidFilmer.com wrote:
> > 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:umper; $Data:umper::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',
};

 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      01-10-2006

it_says_BALLS_on_your forehead wrote:
> use...@DavidFilmer.com wrote:
> > 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:umper; $Data:umper::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);

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-10-2006
use...@DavidFilmer.com wrote:
> 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:umper; $Data:umper::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

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-10-2006

Larry wrote:
> it_says_BALLS_on_your forehead wrote:
> > use...@DavidFilmer.com wrote:
> > > 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:umper; $Data:umper::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...

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      01-10-2006
wrote:
> 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.

--
http://DavidFilmer.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57