Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Push array to hash

by clueless newbie (Curate)
on Jun 24, 2020 at 20:10 UTC ( [id://11118460] : note . print w/replies, xml ) Need Help??


in reply to Push array to hash

Set $Data::Dumper::Deepcopy to true before dumping.

#!/usr/bin/env perl use Data::Dumper; use strict; use warnings; my %hash=("one two"=>"buckle my shoe","three four"=>"shut the door"); my @arrayOfHashes; for (1..3) { push @arrayOfHashes,\%hash; } warn 'BEFORE: ',Data::Dumper->Dump([\@arrayOfHashes],[qw(*arrayOfHashe +s)]),' '; $Data::Dumper::Deepcopy=1; warn 'AFTER: ',Data::Dumper->Dump([\@arrayOfHashes],[qw(*arrayOfHashes +)]),' ';
BEFORE: @arrayOfHashes = ( { 'one two' => 'buckle my shoe', 'three four' => 'shut the door' }, $arrayOfHashes[0], $arrayOfHashes[0] ); at DataDumperDeepCopy.t line 12. AFTER: @arrayOfHashes = ( { 'one two' => 'buckle my shoe', 'three four' => 'shut the door' }, { 'one two' => 'buckle my shoe', 'three four' => 'shut the door' }, { 'one two' => 'buckle my shoe', 'three four' => 'shut the door' } ); at DataDumperDeepCopy.t line 14.

Replies are listed 'Best First'.
Re^2: Push array to hash
by haukex (Archbishop) on Jun 25, 2020 at 06:41 UTC
    Set $Data::Dumper::Deepcopy to true before dumping.

    Caveat: That only changes the way the data structure is displayed, but not the underlying data structure, that will still contain multiple references to the same hash.

Re^2: Push array to hash
by Anonymous Monk on Jun 25, 2020 at 09:24 UTC
    Icky

    warn 'BEFORE: ',Data::Dumper->Dump([\@arrayOfHashes],[qw(*arrayOfHash +es)]),' '; $Data::Dumper::Deepcopy=1; warn 'AFTER: ',Data::Dumper->Dump([\@arrayOfHashes],[qw(*arrayOfHashes +)]),' ';

    ok

    warn 'BEFORE: ',Data::Dumper->Dump([\@arrayOfHashes],[qw(*arrayOfHash +es)]),' '; warn 'AFTER: ',Data::Dumper->new([\@arrayOfHashes],[qw(*arrayOfHashes) +])->Deepcopy(1)->Dump,' ';

    ok? 😌