Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, Hoping you can help. I have a hash of strings which I want to sort by string length ( which I can do). However I also want to print out the hash key. Thanks.
foreach my $value ( sort { length($b) <=> length($a) } values %has +h ) { print "Value: $value\n"; print "Key: ???\n"; }

Replies are listed 'Best First'.
Re: Print key when sorting hash by value
by choroba (Cardinal) on Sep 26, 2014 at 16:08 UTC
    Sort the keys by the lengths of the values:
    for my $key (sort { length($hash{$a}) <=> length($hash{$b}) } keys %ha +sh) { print "$key : $hash{$key}\n"; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks for your speedy reply! You're the best, thanks!