http://qs321.pair.com?node_id=11109739


in reply to Alternative to sort Hash

It looks like SpeechSynthesizer.GetInstalledVoices returns something like an array, and you converted that to a hash pointing to array indexes. Why not simply use the array, and let List::Util's first give you the index directly? For your problem with index 0, replace the first array element with something unmatchable, and if first returns undef, it's the case where you have to use 0 anyway.
my @voices = ...(whatever you do to get the voices array) my $voicezero = $voices[0]; # if name of voice needed later $voices[0] = 'least-liked'; my $voice = first { m/$language/ } @voices || 0; $voices[0] = $voicezero; # if name of voice needed later

Replies are listed 'Best First'.
Re^2: Alternative to sort Hash
by AnomalousMonk (Archbishop) on Dec 06, 2019 at 21:23 UTC

    It could be even simpler than that. Assuming operations on an array, if one can get the array into a function and decide on a regex to match the desired language:

    sub func { ... return unless my @wanted = grep m{ $rx_wanted }xms, @voices; return $wanted[ -1 ]; }
    func returns undef if no desired language is found, the last language if more than one is found, and the first if there is only one. If one didn't want the last language in the case of more than one, maybe something like
        return $wanted[ @wamted > 1 ? 1 : 0 ];


    Give a man a fish:  <%-{-{-{-<