Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Array of Hashes to Hash of arrays for SQL::Abstract

by BillKSmith (Monsignor)
on Mar 18, 2020 at 17:48 UTC ( [id://11114440]=note: print w/replies, xml ) Need Help??


in reply to Array of Hashes to Hash of arrays for SQL::Abstract

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.

Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found