I need your expertise in this apparently small problem
Given the following hash (installed TTS voices available on Windows 10):
%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 };
I need to match a voice by its language. If there are more voices for one language, I just need to pick one. I do this as follows:
use List::Util qw<first>; my $language='English'; my $voice = $voices{ ( first { m/$language/ } keys %voices ) || '' };
So far so good. It pickis the first voice, in the above 0. Now the problem: for misterious Windows reasons, if there are more English Voices, the voice 0 can get in conflict with other voices, with the effect of Windows using the wrong one. My temporary and probably idea is to "jump" 0 if there are other English voices. If hashes would be sortable, I would simply sort the has by its value (from big to small).
%voices = { 'Microsoft Huihui Desktop - Chinese (Simplified)' => 5 'Microsoft Haruka Desktop - Japanese' => 4, 'Microsoft Hedda Desktop - German' => 3, 'Microsoft David Desktop - English (United States)' => 2, 'Microsoft Hazel Desktop - English (Great Britain)' => 1, 'Microsoft Zira Desktop - English (United States)' => 0, };
With a hash ordered like this, if there are more than 2 English voices, "0" will never be picked up.
Any compact idea I am unable to think of?
In reply to Alternative to sort Hash by IB2017
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |