G'day Skeeve,

"Is there a better way to achieve this transposition?"

The following code generates a list of unique keys once and doesn't require initialisation of %columns. Note that the source data I've used contains existent and non-existent keys along with defined and undefined values; for future reference, please consider providing more representative data in your OP.

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my $data = [ { a => 1 }, { a => 3, b => 4 }, { }, { b => 8 }, { a => undef, c => 9 }, ]; my (%columns, %seen_key); my @uniq_keys = grep !$seen_key{$_}++, map keys(%$_), @$data; for my $row (@$data) { push @{$columns{$_}}, $row->{$_} for @uniq_keys; } dd \%columns;

That outputs:

{ a => [1, 3, undef, undef, undef], b => [undef, 4, undef, 8, undef], c => [undef, undef, undef, undef, 9], }

If you want something other than undef to represent your null values — a zero-length string for instance — you can change the push to this if you have Perl 5.10 or later:

push @{$columns{$_}}, $row->{$_} // '' for @uniq_keys;

For older versions of Perl, you can use this:

push @{$columns{$_}}, defined($row->{$_}) ? $row->{$_} : '' for @uniq_ +keys;

Both of those produce identical output:

{ a => [1, 3, "", "", ""], b => ["", 4, "", 8, ""], c => ["", "", "", "", 9], }

As a side note, code like this:

my $sql= new SQL::Abstract( array_datatypes => 1 );

is discouraged and it is recommended that it be avoided. See "perlobj: Indirect Object Syntax" for details, paying particular attention to the opening, emboldened text:

"Outside of the file handle case, use of this syntax is discouraged as it can confuse the Perl interpreter. See below for more details."

A better way to write that statement would be:

my $sql = SQL::Abstract::->new(array_datatypes => 1);

— Ken


In reply to Re: Array of Hashes to Hash of arrays for SQL::Abstract by kcott
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.