# %columns will become the hash of arrays my %columns; # Foreach row of data collect its keys and create an empty array in %columns $columns{$_} //= [] foreach map { keys %$_ } @$data; # Then, for each row foreach my $row ( @$data ) { # push the value foreach column push @{$columns{$_}}, $row->{$_} foreach keys %columns; }