#!/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; #### { a => [1, 3, undef, undef, undef], b => [undef, 4, undef, 8, undef], c => [undef, undef, undef, undef, 9], } #### push @{$columns{$_}}, $row->{$_} // '' for @uniq_keys; #### push @{$columns{$_}}, defined($row->{$_}) ? $row->{$_} : '' for @uniq_keys; #### { a => [1, 3, "", "", ""], b => ["", 4, "", 8, ""], c => ["", "", "", "", 9], } #### my $sql= new SQL::Abstract( array_datatypes => 1 ); #### my $sql = SQL::Abstract::->new(array_datatypes => 1);