Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Alternative to sort Hash

by soonix (Canon)
on Dec 06, 2019 at 10:47 UTC ( #11109739=note: print w/replies, xml ) Need Help??


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 (Bishop) 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109739]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2020-05-26 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (150 votes). Check out past polls.

    Notices?