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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.