in reply to Re^2: Alternative to sort Hash
in thread Alternative to sort Hash
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]; }
|
---|