in reply to Alternative to sort Hash

Any compact idea I am unable to think of?

Just remove the 0-valued entry from the hash before you start? That's a simple way not to choose it.

It isn't entirely clear to me from your description precisely what it is you want to achieve, unfortunately. Perhaps a test would help? ie: How to ask better questions using Test::More and sample data

Replies are listed 'Best First'.
Re^2: Alternative to sort Hash
by IB2017 (Pilgrim) on Dec 06, 2019 at 09:58 UTC

    I cannot remove "0". If on the Windows machine there is only one English voice installed, I need to pick up this (and it will be 0). I simply need to pick up any English voice which is not 0 if other English voices are there.

      So this, then?

      use strict; use warnings; use Test::More tests => 4; my $voices = { 'Microsoft Hedda Desktop - German' => 3, 'Microsoft Haruka Desktop - Japanese' => 4, 'Microsoft Zira Desktop - English (United States)' => 0, 'Microsoft Hazel Desktop - English (Great Britain)' => 1, 'Microsoft David Desktop - English (United States)' => 2, 'Microsoft Huihui Desktop - Chinese (Simplified)' => 5 }; my $engtest = bestvoice ('English', $voices); cmp_ok $voices->{$engtest}, '>', 0, 'English has positive value'; like $engtest, qr/English/, 'English matched in key'; is bestvoice ('German', $voices), 'Microsoft Hedda Desktop - German', 'German matched in key'; is bestvoice ('Esperanto', $voices), undef, 'Esperanto not matched'; sub bestvoice { my ($want, $v) = @_; my @poss = grep {/$want/} keys %$v; if ($#poss > 0) { @poss = grep { $v->{$_} } @poss; } return $poss[0]; }
      So do that exactly? sub pickVoice... No hash thought-jam but simple api