IB2017 has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Alternative to sort Hash
by soonix (Chancellor) on Dec 06, 2019 at 10:47 UTC | |
by AnomalousMonk (Archbishop) on Dec 06, 2019 at 21:23 UTC | |
Re: Alternative to sort Hash
by kcott (Archbishop) on Dec 06, 2019 at 12:04 UTC | |
by soonix (Chancellor) on Dec 06, 2019 at 14:11 UTC | |
Re: Alternative to sort Hash
by hippo (Archbishop) on Dec 06, 2019 at 09:55 UTC | |
by IB2017 (Pilgrim) on Dec 06, 2019 at 09:58 UTC | |
by hippo (Archbishop) on Dec 06, 2019 at 11:30 UTC | |
by Anonymous Monk on Dec 06, 2019 at 10:10 UTC |