'key1' => ["A","B","C","D"], 'key2' => ["A","C","B","C"], 'key3' => ["C","A","D","H"], 'key4' => ["A","B","I","C"], #### [ "A", "A", "A", "A" ], [ "B", "B", "-", "B" ], [ "C", "C", "-", "C" ], [ "D", "-", "D", "-" ], #### 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; } } } }