Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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?
[stevieb]: I have got ready everything in a stable manner to go forward with a full-blown "Raspberry Pi with Perl" series, and at the same time "Automated testing locally for everything" series. A year it took. A year I've been waiting to do this.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-06-29 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (653 votes). Check out past polls.