I am finding the experimental
for_list feature in v5.36 very useful - mostly when combined with the
indexed builtin or when processing hashes. However, I do frequently want to process hash pairs sequentially by key (similar to
for my $k (sort keys %h) { ... }) so I set about looking for a way to do it in a for_list. The best I've come up with is:
#!/usr/bin/env perl
use v5.36;
no warnings q/experimental::for_list/;
use List::AllUtils qw/pairs unpairs/;
my %h = map { ( $_, rand ) } ('a' .. 'z');
for my ($k, $v) ( unpairs (sort { $a->key cmp $b->key } pairs %h) ) {
say "$k => $v";
}
which is too verbose to be practical. I noticed reading the doc for
key/value pair list functions the following:
NOTE: At the time of writing, the following pair* functions that take a block do not modify the value of $_ within the block, and instead operate using the $a and $b globals instead. This has turned out to be a poor design, as it precludes the ability to provide a pairsort function. Better would be to pass pair-like objects as 2-element array references in $_, in a style similar to the return value of the pairs function. At some future version this behaviour may be added.
Until then, users are alerted NOT to rely on the value of $_ remaining unmodified between the outside and the inside of the control block.
I don't understand the issue they're raising, but it does sound like a
pairsort is what I need. In the meantime, can anyone suggest a better way to sort the pairs when iterating with a for_list over a hash?