Is the problem that the hash is in a random order? It says on page 8 of the Camel book that: "if you use a hash in list context, it 'll convert the hash back to a list of key/value pairs, in a weird order. However, page 182 points out that you can order the output by value simply by saying: foreach $key (sort {$myhash{b} <=>$myhash{a} } keys %myhash) {printf "%4d %s\n", $myhash{$key},$key;}