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 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 imbibing at the Monastery: (7)
As of 2015-07-03 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls