Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Array of Hashes to Hash of arrays for SQL::Abstractby kcott (Archbishop) |
on Mar 19, 2020 at 07:46 UTC ( [id://11114456]=note: print w/replies, xml ) | Need Help?? |
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.
That outputs:
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:
For older versions of Perl, you can use this:
Both of those produce identical output:
As a side note, code like this:
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:
— Ken
In Section
Seekers of Perl Wisdom
|
|