sub meld_columns { my ($arrays, @arrNames) = @_; my ($maxIdx) = sort { $b<=>$a } map {$#$_} @{$arrays}{@arrNames}; my @outarray = map { my $i = $_; # keep track of the current "row" to use in inner map block # get the respective element from each array. # If we're beyond the individual array length, just use "\t". # Also glue all cols together w/a "\t" join "\t", map { $i <= $#$_ ? $_->[$i] : "\t" } @{$arrays}{@arrNames} } 0..$maxIdx; } my %arrayHolder = map { $_ => [] } 'a' .. 'z'; # build the initial data structure (instead of named arrays) my @out = meld_columns( \%arrayHolder, 'a' .. 'h' ); # meld just A through H my @out = meld_columns( \%arrayHolder, 'f', 'a', 'c' ); # meld just F A C, in that order.