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: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|