dclone and copying one element of a hash

Discussion in 'Perl Misc' started by TonyShirt, May 4, 2004.

  1. TonyShirt

    TonyShirt Guest

    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?
    TonyShirt, May 4, 2004
    #1
    1. Advertising

  2. (TonyShirt) wrote in
    news::

    > 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__




    --
    A. Sinan Unur
    (reverse each component for email address)
    A. Sinan Unur, May 4, 2004
    #2
    1. Advertising

  3. TonyShirt

    TonyShirt Guest

    "A. Sinan Unur" <> wrote in message news:<Xns94DEE0EBEBF6Fasu1cornelledu@132.236.56.8>...
    > (TonyShirt) wrote in
    > news::
    >
    > > 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__


    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?
    TonyShirt, May 7, 2004
    #3
    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. Replies:
    3
    Views:
    2,415
    Jim Gibson
    Mar 29, 2005
  2. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  3. Replies:
    14
    Views:
    222
    Tomi Häsä
    Jan 10, 2005
  4. A. Sinan Unur

    dclone alternatives

    A. Sinan Unur, Feb 23, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    91
    A. Sinan Unur
    Feb 24, 2006
  5. Graham Wood

    where to put dclone

    Graham Wood, Sep 19, 2006, in forum: Perl Misc
    Replies:
    8
    Views:
    111
    Graham Wood
    Sep 22, 2006
Loading...

Share This Page