Perl
  Home arrow Perl arrow Page 3 - Perl Lists: A Final Look at List::Util
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl Lists: A Final Look at List::Util
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-21

    Table of Contents:
  • Perl Lists: A Final Look at List::Util
  • Min(List)...Your Guide to Minimalism
  • MinStr(List) Like Them Small
  • Do the Shuffle

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Perl Lists: A Final Look at List::Util - MinStr(List) Like Them Small
    (Page 3 of 4 )

    This subroutine works like min(), only for strings. It will find the smallest valued string in a list, and return it. Here are some examples of its use:


    #!/usr/bin/perl

    use List::Util qw(minstr);

    @Letters=('A','B','C','D');

    $Small=minstr(@Letters);

    print $Small;

    In the above example, we call the minstr() sub near the top, then create a list, @Letters, and give it some values. Next, we use minstr() to extract the smallest value in the list and store it in our $Small variable. Finally, we print out the result:

      A

    As you may recall from our maxstr() examples in the previous article, the values 'A' and 'a' are not equal. Consider this code:


    #!/usr/bin/perl

    use List::Util qw(minstr);

    @Letters=('A','a','B','b','C','c','D','d');

    $Small=minstr(@Letters);

    print $Small;

    Which value will be the lowest here? If you guessed "A," then you are correct. Always remember that uppercase letters in Perl are worth less than lowercase values. Let's see how they fare against shifted characters:



    #!/usr/bin/perl

    use List::Util qw(minstr);

    @Letters=('A','a','B','b','C','c','D','d','!','@','#','$');

    $Small=minstr(@Letters);

    print $Small;

    In this example, the smallest result is the exclamation point(!).

    Next we will look at whole strings and see how they compare:


    #!/usr/bin/perl

    use List::Util qw(minstr);

    @Letters=('Apple','Pie','999','@#$%!');

    $Small=minstr(@Letters);

    print $Small;

    In this situation, '999' is the smallest value.

    Finally, let's test a-z, A-Z, 1-100, and some more shift characters to see which one has the least value:


    #!/usr/bin/perl

    use List::Util qw(minstr);

    @Letters=('A'..'Z','a'..'z','1'..'100','!','@','#','$','%','&','+');

    $Small=minstr(@Letters);

    print $Small;

    Again, our smallest value is the exclamation point(!).

    More Perl Articles
    More By James Payne


       · Thanks for stopping by for another article in our series on Perl Lists and Hashes....
     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway