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

Re: Putting an array into a hash gives an unexpected reference to the hash itself.

by Perlbotics (Abbot)
on Nov 07, 2012 at 23:29 UTC ( #1002769=note: print w/ replies, xml ) Need Help??


in reply to Putting an array into a hash gives an unexpected reference to the hash itself.

Sure, they are the same. You assigned the same reference twice.

If you want two different copies, use two different references. E.g. the original \@maintainers and a copy [ @maintainers ] or two new copies.

#!/usr/local/bin/perl -w use strict; use warnings; use Data::Dumper; my @maintainers = qw/ worker@company.com manager@company.com /; my %services = ( service1 => { email => [ @maintainers ] }, # shallow copy of @main +tainers service2 => { email => [ @maintainers ] }, # another shallow ... ); print Data::Dumper->Dump( [ \%services ], [qw/*services/] ); exit 0; __DATA__ %services = ( 'service1' => { 'email' => [ 'worker@company.com', 'manager@company.com' ] }, 'service2' => { 'email' => [ 'worker@company.com', 'manager@company.com' ] } );

One more thing. I was creating a shallow copy in the example above. For more complex data structures, you might need to copy recursively or use something like Storable::dclone or Clone.


Comment on Re: Putting an array into a hash gives an unexpected reference to the hash itself.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-07-29 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls