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

Almost
1 7 2 8 3 9 4 10 5 11 Use of uninitialized value in printf [...]. 6
Maybe
my @keys = sort { $hash{$b} <=> $hash{$a} || $b cmp $a } keys %hash; my $half = int($#keys / 2); printf "%s\t%s\n", @keys[$_, $_+$half] for 0 .. $half-1; printf "%s\n", $keys[$#keys] unless $#keys % 2;

Replies are listed 'Best First'.
Re^3: split hash into two columns
by Anonymous Monk on Mar 27, 2006 at 20:30 UTC
    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.

      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;