Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Finding the Shortest Element in an Array

by haoess (Curate)
on Oct 13, 2005 at 20:07 UTC ( #500031=note: print w/replies, xml ) Need Help??

in reply to Finding the Shortest Element in an Array

@array = qw( hello, superduper, hi )

Maybe it's a mistake, but this creates a list containing three comma ending strings.

For your algorithm you should have a look at the implementation of minmax in List::MoreUtils. It should be simple to adjust it to check for the length of a string. The documentation says:

The minmax algorithm differs from a naive iteration over the list where each element is compared to two values being the so far calculated min and max value in that it only requires 3n/2 - 2 comparisons. Thus it is the most efficient possible algorithm.


Replies are listed 'Best First'.
Re^2: Finding the Shortest Element in an Array
by Limbic~Region (Chancellor) on Oct 13, 2005 at 20:32 UTC
    Actually, using List::Util's 'reduce' would be better. The minmax algorithm is doing more work then it needs because it is trying to identify both the min and the max where here only the min is desired. So in addition to List::Util being part of the core, it is also XS in most environments and implements the watermark algorithm which for this is much more efficient than 'minmax';

    Cheers - L~R

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://500031]
[choroba]: Yes, sometimes missing ; after the use clause just interprets the next line somehow and passes the result to the used module which could happily ignore it. Been there, seen that.
[Discipulus]: luckly the program was 20 lines!

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-02-28 08:53 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (398 votes). Check out past polls.