Art_XIV has asked for the wisdom of the Perl Monks concerning the following question:
I have a HoAoH as in the following distilled example:
use strict; use warnings; use Data::Dumper; my %types = ( TYPE1 => [ { NAME => 'Joe', ID => '022'}, { NAME => 'Sue', ID => '088'}, { NAME => 'Tom', ID => '108'} ], TYPE2 => [ { NAME => 'Sue_B', ID => '089'}, { NAME => 'Sue_C', ID => '090'} ] ); splice @{$types{TYPE1}}, 1, 1, @{$types{TYPE2}}; print Dumper(\%types)
I am trying to replace the 'Sue' hash element in the array referenced by TYPE1 with both of the hash elements in the array referenced by TYPE2.
It seems to me that the splice should do the trick, but doesn't, producing the following output instead:
$VAR1 = { 'TYPE2' => [ { 'ID' => '089', 'NAME' => 'Sue_B' }, { 'ID' => '090', 'NAME' => 'Sue_C' } ], 'TYPE1' => [ { 'ID' => '022', 'NAME' => 'Joe' }, $VAR1->{'TYPE2'}[0], $VAR1->{'TYPE2'}[1], { 'ID' => '108', 'NAME' => 'Tom' } ] };
I am attempting to splice because I want to preserve the order of the elements in the arrays. Can anyone clue me in to what I am doing wrong and what a proper way of doing this would be?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Slicing a HoAoH
by edan (Curate) on Apr 28, 2004 at 15:24 UTC | |
|
Re: Slicing a HoAoH
by dreadpiratepeter (Priest) on Apr 28, 2004 at 15:25 UTC | |
|
Re: Slicing a HoAoH
by rinceWind (Monsignor) on Apr 28, 2004 at 15:35 UTC | |
|
Re: Slicing a HoAoH
by Roy Johnson (Monsignor) on Apr 28, 2004 at 15:37 UTC | |
|
Re: Slicing a HoAoH
by artist (Parson) on Apr 28, 2004 at 15:38 UTC | |
|
Re: Slicing a HoAoH
by Art_XIV (Hermit) on Apr 28, 2004 at 15:38 UTC | |
|
Re: Slicing a HoAoH
by eric256 (Parson) on Apr 28, 2004 at 15:35 UTC |