It accommodates for multiple key/values in data2.

It accommodates multiple hashes in  $data2 with each hash having multiple key/values! This exceeds the implied requirement of the OP, but I find that requirements tend to expand spontaneously in these situations anyway.

I was a bit daunted by the time-complexity of the triple nesting of your solution. I tried to come up with some alternatives. I think these all work, but neither your solution nor any of mine have been thoroughly tested by me. Unfortunately, I think the big-Os of all these solutions, yours and mine, are going to turn out to be exactly the same!

foreach my $hashref (@$data1) { %$hashref = (%$hashref, %$_) for @$data2; } for my $hash1 (@$data1) { @{ $hash1 }{ keys %$_ } = values %$_ for @$data2; } foreach my $hash1 (@$data1) { for my $hash2 (@$data2) { map { $hash1->{$_} = $hash2->{$_} } keys %$hash2; } } for my $hash1 (@$data1) { for my $hash2 (@$data2) { while (my ($k2, $v2) = each %$hash2) { $hash1->{$k2} = $v2; } } }


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: Insert into element into arrays ref by AnomalousMonk
in thread Insert into element into arrays ref by Anonymous Monk

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.