Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Perlbotics (Chancellor)
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002769]
help
Chatterbox?
NodeReaper practices exenteration where nobody will notice

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-08-19 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (311 votes). Check out past polls.

    Notices?