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

However, I would probably try to loop over $data just once

The disadvantage here is, that you might loose null-values due to missing keys in your data

my $data = [ {a => 1, b => 2, c => 11}, {a => 3, c => 12}, {a => 5, b => 6} ];

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^3: Array of Hashes to Hash of arrays for SQL::Abstract
by Veltro (Hermit) on Mar 18, 2020 at 16:27 UTC

    I think you are right, in such circumstance I think I would use an index:

    Something like:

    for my $i (0..$#{$data}) { foreach ( keys %{$data->[$i]} ) { $columns->{$_}->[$i] = $data->[$i]->{$_} ; } ; }

    Also makes it fairly easy to see what happens:

    $columns->{$_}->[$i] = $data->[$i]->{$_} ; hash------^-------------------------^ array-----------^-------------^
Re^3: Array of Hashes to Hash of arrays for SQL::Abstract
by haukex (Archbishop) on Mar 19, 2020 at 09:52 UTC
    The disadvantage here is, that you might loose null-values due to missing keys in your data

    ... which you didn't mention at all in the root node. Changing the specs on us is not nice :-P Is this example you've shown now representative, or are there other edge cases?

      ... which you didn't mention at all in the root node

      You are right. This is because it was obvious to me. I think this happens to every programmer.

      Sorry.

      or are there other edge cases

      None that I know of.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e