in reply to Finding the Shortest Element in an Array
I would simply loop over the elements comparing each stringlength with the current minimal length.
While using all the bells and whistles with map and shift and sort and such, I think it's pretty overdone. Just as plain as this untested:
use strict; use warnings; my @array = qw( hello, superduper, hi ); # You know that those commas are part of the strings!? my $shortest= $array[0]; my $shortest_length= length $shortest; for ($i= $#array; $i; --$i) { my $length= length $array[$i]; if ($length < $shortest_length) { $shortest_length= $length; $shortest= $array[$i]; } } print $shortest,"\n";
Of course, one can shorten this, but you wanted a direction, not a "nice" solution...
|
|---|