http://www.perlmonks.org?node_id=212235

in reply to Re: The longest word ....
in thread The longest word ....

Caveats noted, the problem wasn't "the longest word" but the longest words, which is either

• Two passes.

1 to get the length, one extract the words of that length.

• Requires building a hash (or similar) to record the lengths and the words.

The former would work on a list greater than memory, the latter is likely to fail before the sort.

Anyone who "throws a wordlist" at an algorithm without understanding its limitations, gets what they deserve.

In the context of the requested solution: A shorter way of finding the longest words in a supplied string (already memory bound) using sort was just a quick option. I didn't read the question as requiring a failsafe nor scalable solution. Did you?

After all

print@_{sort keys %_},\$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r\$s-t%t#u'

is probably not the most efficient way of printing "just another bofh" either.

Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Replies are listed 'Best First'.
Re: Re: Re: The longest word ....
by Anarion (Hermit) on Nov 12, 2002 at 12:54 UTC
Another point of view without using sort:

```sub lword{
push(@{\$a[length]},\$_)for(pop)=~/\b\w+/g;@{\$a[-1]}
}
```sub lword2{