Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Finding the Shortest Element in an Array

by injunjoel (Priest)
on Oct 13, 2005 at 19:40 UTC ( #500017=note: print w/replies, xml ) Need Help??


in reply to Finding the Shortest Element in an Array

Greetings all,
#!/usr/bin/perl -w use strict; my @array = qw(hello superduper hi); #our good friend the ST (aka: Schwartzian Transform) my $shortest = shift @{[ map{$_->[1]} sort{$a->[0] <=> $b->[0]} map{[length $_, $_]}@array ]}; print $shortest;
Hope that helps. Here is the documentation on what is going on with the Schwartzian Transform.

-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Replies are listed 'Best First'.
Re^2: Finding the Shortest Element in an Array
by Limbic~Region (Chancellor) on Oct 13, 2005 at 20:14 UTC
    injunjoel,
    I am not sure why you suggested sorting a list to find the shortest element. A watermark algorithm should be used since sorting does far more work then is necessary. I would have suggested List::Util's 'reduce' function or a hand rolled one as I demonstrated in my How A Function Becomes Higher Order tutorial.

    Cheers - L~R

      Perhaps sort was chosen because using sort is easy on the person writing the code. That was the first thing that came to my mind -- just sort the list based on length, but i see no reason to use a Schwartzian Transform here. This should suffice, and while it may take longer and use more memory, it sure looks neat:

      @array = sort { length($a) <=> length($b) } @array;
      Code like that reminds me of my days painting dorm rooms. My boss would tell me "Let the brush do the work, not you." And yes, that would include using List::Util, FWIW.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        While I don't care for the idea of using sort for this, if you are going to use it, doing it like you did and avoiding the ST probably makes a lot of sense. It should take less memory, not more. And, since length() is very fast it probably won't lose by much on performance either (it might even win.) And, you just can't beat the clarity of the code.

        -sauoq
        "My two cents aren't worth a dime.";
        
      Limbic~Region
      Looking back over my suggestion I guess the answer would be partly because sort seemed appropriate, as jeffa stated (nice sort suggestion by the way, much cleaner) and partly because of an idiosyncratic tendancy to over-use the ST.
      I've been writing more VB code than I would like to lately and I think this is just a subconscious backlash, my mind saying "Give me my programmatic flexibility back!!".

      -InjunJoel
      "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2019-07-15 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?