Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Copy of an anonymous hash?

by holli (Abbot)
on Apr 26, 2005 at 07:33 UTC ( #451438=note: print w/replies, xml ) Need Help??

in reply to Copy of an anonymous hash?

my $new_element = {%{$hash_array[$index]}};

holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Copy of an anonymous hash?
by DrHyde (Prior) on Apr 26, 2005 at 09:37 UTC
    That works, but only if the hash contains simple scalars. If it contains any references, the reference will be copied, so you'll have two references to the same thingy. If that matters, I recommend using the Storable module and its dclone function.

    This won't solve the problem of references to items outside the structure, but that's not usually a problem that needs solving anyway.

Re^2: Copy of an anonymous hash?
by tphyahoo (Vicar) on Apr 26, 2005 at 14:16 UTC
    I believe
    my $new_element = \%{$hash_array[$index]};
    would also work. (Right?)

    UPDATE: I meant, my referencing syntax would work as well as hollis. This is right... right?

      Nope, it'll still point to the same underlying hash.

      freebie:~ 702> perl -le '$a={qw(a b)};$b=\%{$a};print "$a\t$b"' HASH(0x804c844) HASH(0x804c844)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://451438]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-03-30 23:55 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.