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

Re: The longest word ....

by dingus (Friar)
on Nov 12, 2002 at 07:24 UTC ( [id://212213]=note: print w/replies, xml ) Need Help??


in reply to The longest word ....

Yet, my perl issue is that I really wonder what would be a shorter solution for my longest_words routine.

I think you want to do the hash the other way around. I.e. make it a hash of lengths and push each word onto an anonymous array of words of than length:

sub longest_words { my %W; push (@{$W{length($_)}}, $_) for (split /\s+/, join ' ', @_); return @{$W{ (sort{$b <=>$a} keys %W)[0]} }; }
Notes: If you do a print Dumper (\%W); before the return you will see the hash that has been constructed.
The (sort{$b <=>$a} keys %W)[0] returns the key with the highest numerical value.

Dingus


Enter any 47-digit prime number to continue.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-18 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found