which is too verbose to be practical. I noticed reading the doc for key/value pair list functions the following:#!/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"; }
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?
In reply to for_list and sorting key/value hash pairs by ibm1620
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |