Make a copy of structure within of a HoH?

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jan 10, 2006.

  1. Guest

    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!

    --
    http://DavidFilmer.com
     
    , Jan 10, 2006
    #1
    1. Advertising

  2. 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::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.
     
    it_says_BALLS_on_your forehead, Jan 10, 2006
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead wrote:
    > 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::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',
    };
     
    it_says_BALLS_on_your forehead, Jan 10, 2006
    #3
  4. Larry Guest

    it_says_BALLS_on_your forehead wrote:
    > 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::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);
     
    Larry, Jan 10, 2006
    #4
  5. Paul Lalli Guest

    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::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
     
    Paul Lalli, Jan 10, 2006
    #5
  6. Larry wrote:
    > it_says_BALLS_on_your forehead wrote:
    > > 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::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...
     
    it_says_BALLS_on_your forehead, Jan 10, 2006
    #6
  7. Guest

    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
     
    , Jan 10, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Greenfield

    HoH and MLDBM problems

    Brian Greenfield, Aug 26, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    140
    Bob Walton
    Sep 17, 2003
  2. Aaron DeLoach

    HoHoH (hash of HoH's)

    Aaron DeLoach, Jul 18, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    166
    Aaron DeLoach
    Jul 20, 2004
  3. Replies:
    1
    Views:
    109
    Gunnar Hjalmarsson
    Jan 17, 2005
  4. Replies:
    2
    Views:
    99
  5. lynn
    Replies:
    5
    Views:
    114
    John W. Krahn
    Jul 12, 2005
Loading...

Share This Page