while (keys %h < $n) { $h{select_thing()}++; } @things = keys %h;
In scalar context, keys %h is super-efficient.
No, it does not count the keys one at a time.