Without any missing keys, the transformation is fairly straightforward. You do not need to create an empty array first.
use warnings; use strict; use Data::Dump qw(pp); my $data = [ {a => 1, b => 2}, {a => 3, b => 4}, {a => 5, b => 6}, ]; # create data struct like this # # my $required = { a => [1, 3, 5], b => [2, 4, 6],}; my %required; foreach my $href (@$data) { push @{$required{$_}}, $href->{$_} foreach keys %$href; } pp \%required; #{ a => [1, 3, 5], b => [2, 4, 6] }
I don't see the requirement for missing keys in the OP? If that is true, then I would probably make 2 passes over $data, the first to discover the "universe of keys" so that appropriate null or undef values can be filled into the arrays as they are generated. There is also the question of what kind of DB you are using and what if any "default" values are specified during table creation? It could also be that SQL::Abstract is not the best way to do what you want?

Update: Some code to implement the above.

### assume that some keys could be missing or others added my $data2 = [ {a => 1, }, {a => 1, b => 2}, {a => 3, b => 3, c=>7}, {a => 5, b => 6, c=>8}, {a => 5, c=>8, d=>9}, ]; my %key_universe_h; my $default =0; # determine all possible keys (number of columns) foreach my $href (@$data2) { $key_universe_h{$_}++ foreach keys %$href; } my @key_universe_a = sort (keys %key_universe_h); # proceed as in first example, but with default # for non-existant key values my %required2; foreach my $href (@$data2) { foreach my $column (@key_universe_a) { my $value = $default; $value = $href->{$column} if exists $href->{$column}; push @{$required2{$column}}, $value; } } pp $data2; pp \%required2;
Prints:
[ { a => 1 }, { a => 1, b => 2 }, { a => 3, b => 3, c => 7 }, { a => 5, b => 6, c => 8 }, { a => 5, c => 8, d => 9 }, ] { a => [1, 1, 3, 5, 5], b => [0, 2, 3, 6, 0], c => [0, 0, 7, 8, 8], d => [0, 0, 0, 0, 9], }

In reply to Re: Array of Hashes to Hash of arrays for SQL::Abstract by Marshall
in thread Array of Hashes to Hash of arrays for SQL::Abstract by Skeeve

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.