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?

Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

In reply to Slicing a HoAoH by Art_XIV

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.