You have not specified
exactly what is required for your 'missing keys' test case. There is a very subtle difference between
$data_hash->{c}->[2] = undef and
$data)hash->{c}->[2] does not exist. I assumed that it should be undef. This requires setting the length of the arrays. The following test passes. Try commenting out the statement with '# Set length'. Observe how the test fails.
use strict;
use warnings;
use Test::More tests=>1;
my $data = [
{a => 1, b => 2, c=>11},
{a => 3, c=>12},
{a => 5, b => 6},
];
my $required = {
a => [1, 3, 5 ],
b => [2, undef, 6 ],
c =>[11, 12, undef],
};
my $data_hash;
foreach my $i (0..$#$data) {
while ((my $key, my $value)= each %{$data->[$i]}) {
$#{$data_hash->{$key}} = $#$data; # Set length
$data_hash->{$key}->[$i] = $value;
}
}
is_deeply($data_hash, $required, 'missing keys');
Of course, if you require c => [11, 12] then the "set length" statement must be omitted.
-
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.