in reply to Re^2: split hash into two columns
in thread split hash into two columns

This is a bit more code than I thought. Why are all the examples of my hash being broken into an array? I'd have to imagine there's a way to calculate 1/2 of the hash or something because in the code above, it doesn't print the key and the value.

Replies are listed 'Best First'.
Re^4: split hash into two columns
by ikegami (Patriarch) on Mar 27, 2006 at 20:41 UTC

    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;