Perl
  Home arrow Perl arrow Page 4 - 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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    Perl Lists: Utilizing List::Util - Max()ing It Out


    (Page 4 of 5 )

    The Max function is used to return the maximum value in a list. It's that simple. Here it is at work:


    #!/usr/bin/perl

    use List::Util qw(max);

    @Nums=(1,900,50,22);

    $High=max(@Nums);

    print $High;

    This goes through our list @Nums, looks for the highest number, and places it in the $high variable. Here is the obvious result:

      900

    As before, this bad boy can be used in conjunction with operators, statements, etc. Here it is with an if...else:


    #!/usr/bin/perl

    use List::Util qw(max);

    @Nums=(1,900,50,22);

    $high=max(@Nums);

    if($high > 25) {print "The number $high is greater than 25!"}

    else {print "The value $high is less than 25!"};

    This will print out:

      The value 900 is greater than 25!

    We can also perform calculations off of our subroutine. Here is a simple addition:


    #!/usr/bin/perl

    use List::Util qw(max);

    @Nums=(1,900,50,22);

    $high=max(@Nums) + 1;

    print $high;

    Giving us:

      901

    Here is a final example showcasing how you can compare the highest values of two lists:


    #!/usr/bin/perl

    use List::Util qw(max);

    @Nums=(1,900,50,22);

    @Nums2=(90..898);

    $High=max(@Nums);

    $Less=max(@Nums2);

    if($High > $Less) {$Less=$High};

    print $High . "\n";

    print $Less;

    This code creates two arrays. We fill one with some values and we use a sequential operator to fill the other. Next, we use max to find the maximum value in each and store them in their respective variables. Finally, we use an if statement to determine whether $High is greater than $Less. If so, then the value in $Less is given the same value as the variable $High. We then print out the result of both variables:

      900

      900

    You will note that we only used numbers in the previous example. In order to find the highest string value, we use our good buddy and max(List)'s cousin, maxstr(List).

    More Perl Articles
    More By James Payne


       · Thanks for stopping by for another episode in our continuing look at Hashes and...
     

       

    PERL ARTICLES

    - 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
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util





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