Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

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


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

G'day polarbear,

What you are describing is the default behaviour for Data::Dumper. You can change it by setting $Data::Dumper::Deepcopy:

$ perl -Mstrict -Mwarnings -e ' use Data::Dumper; $Data::Dumper::Deepcopy = 1; my @maintainers = qw/ worker@company.com manager@company.com /; my %services = ( service1 => { email => \@maintainers }, service2 => { email => \@maintainers }, ); print Data::Dumper->Dump( [ \%services ], [qw/*services/] ); ' %services = ( 'service1' => { 'email' => [ 'worker@company.com', 'manager@company.com' ] }, 'service2' => { 'email' => [ 'worker@company.com', 'manager@company.com' ] } );

Update: I may have misinterpreted "I don't want the lists connected in any way." as the cross-referencing of the lists output by Data::Dumper rather than the lists pointed to by the arrayrefs. If this is the case, the email => [ @maintainers ] solution would be the way to go. Thanks, frozenwithjoy.

-- Ken


Comment on Re: Putting an array into a hash gives an unexpected reference to the hash itself.
Select or Download Code
Re^2: Putting an array into a hash gives an unexpected reference to the hash itself.
by frozenwithjoy (Curate) on Nov 07, 2012 at 23:39 UTC
    Just a warning though that if you change one (e.g., push $services{"service1"}{"email"}, "xxx";), they will both change:
    %services = ( 'service1' => { 'email' => [ 'worker@company.com', 'manager@company.com', 'xxx' ] }, 'service2' => { 'email' => [ 'worker@company.com', 'manager@company.com', 'xxx' ] } );

      ++ Thanks for that. It looks like I misinterpreted the question. I've updated my response.

      -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-08-22 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls