my @output = (); my @keys = sort keys %hash; my %start = (); @start{@keys} = (0) x scalar(@keys); my $first = shift @keys; my $total_columns = $# { $hash{$first} }; for my $column (0 .. $total_columns) { $output[$column] = [ $hash{$first}[$column] ]; for my $hkey (@keys) { my $aref = $output[$column]; push @$aref, "-"; for my $i ($start{$hkey} .. $total_columns) { if ($aref->[0] eq $hash{$hkey}[$i]) { $aref->[-1] = $aref->[0]; $start{$hkey} = $i + 1; last; } } } }