Perl
  Home arrow Perl arrow Page 5 - Perl Lists: Utilizing List::Util
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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: Utilizing List::Util
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-04-14


    Table of Contents:
  • Perl Lists: Utilizing List::Util
  • First Up On the Block
  • Making First a Little More Complicated
  • Max()ing It Out
  • My String is Bigger Than Yours: The MaxStr(List) Story

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Perl Lists: Utilizing List::Util - My String is Bigger Than Yours: The MaxStr(List) Story
    ( Page 5 of 5 )

    To compare string values and extract the largest, we use maxstr. Below are some examples:


    #!/usr/bin/perl

    use List::Util qw(maxstr);

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

    $Big=maxstr(@Letters);

    print $Big;

    In this simple example, we compare the values of A, B, C, D. Which one is larger?

      D

    Pretty simple right? What about if we try something like this:


    #!/usr/bin/perl

    use List::Util qw(maxstr);

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

    $Big=maxstr(@Letters);

    print $Big;

    Guess which letter has the highest value? The result:

      d

    Lowercase letters in Perl have a higher value than uppercase. What about special characters?


    #!/usr/bin/perl

    use List::Util qw(maxstr);

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

    $Big=maxstr(@Letters);

    print $Big;

    Again, "d" wins the day.

    You will note, of course, that a string can be a whole word and even numbers. Here we compare those:


    #!/usr/bin/perl

    use List::Util qw(maxstr);

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

    $Big=maxstr(@Letters);

    print $Big;

    The largest string value here is:

      Pie

    Lastly, consider this bizarre code, in which we compare the values a-z, A-Z, 1-100, and a bunch of shift characters:


    #!/usr/bin/perl

    use List::Util qw(maxstr);

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

    $Big=maxstr(@Letters);

    print $Big;

    And the winner is....<insert drum roll here>:


      z

    Conclusion

    Well as you can see, we barely scraped the surface in this article. There are four more subroutines left to go, which we should be able to cover in our next tutorial. The min() and minstr() work in a similar way to our max() and maxstr() subroutines. We'll also go over the shuffle() and sum(). So check back soon.

    Till then...



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT