in reply to Re: Alternative to sort Hash
in thread Alternative to sort Hash

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.

Replies are listed 'Best First'.
Re^3: Alternative to sort Hash
by hippo (Archbishop) on Dec 06, 2019 at 11:30 UTC

    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]; }
Re^3: Alternative to sort Hash is API
by Anonymous Monk on Dec 06, 2019 at 10:10 UTC
    So do that exactly? sub pickVoice... No hash thought-jam but simple api