Hash won't do since they are unordered. A list will do for
1 2 3 4 5 6
but an array is needed for
1 4 2 5 3 6
since you need to access elements out of order.
As for not printing the key and the value, it's trivial to make the above print both the key and the value since you have both the key and the hash. The output you asked for wasn't clear, so we only included a minimal print.
Update: Here it is:
my @keys = sort { $hash{$b} <=> $hash{$a} || $b cmp $a } keys %hash; my $half = int($#keys / 2); printf "%s => %s\t%s => %s\n", $keys[$_ ], $hash{$keys[$_ ]}, $keys[$_+$half], $hash{$keys[$_+$half]} for 0 .. $half-1; printf "%s => %s\n", $keys[$#keys], $hash{$keys[$#keys]} unless $#keys % 2;
In reply to Re^4: split hash into two columns
by ikegami
in thread split hash into two columns
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |