Perl
  Home arrow Perl arrow Page 2 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl Lists: Utilizing List::Util - First Up On the Block


    (Page 2 of 5 )

    The first{block}list subroutine, as stated above, returns the first value in a list. If you have a list that contains the following data, in this order: Hercules, Perseus, Theseus, and Mister T (basically a list of Greek heroes) and you use the first{block}list subroutine on it, then your result would be: Hercules. This is because he is the first piece of data in the list. Don't believe it? Let's go to the code:


    #!/usr/bin/perl

    use List::Util qw(first);

    @Greek=('Hercules','Perseus','Theseus','Mister T');

    $Front = first {$_} @Greek;

    print $Front;

    The result?

      Hercules

    We can do the same with numbers:


    #!/usr/bin/perl

    use List::Util qw(first);

    @Nums=(1,2,3,4);

    $Front = first {$_} @Nums;

    print $Front;

    Which results in:

      1

    But what if we don't want just the first value in a list? What if we want to set certain criteria that must be met? We can do so by adding conditionals to our subroutine:


    #!/usr/bin/perl

    use List::Util qw(first);

    @Nums=(1,2,3,4);

    $Front = first {$_>2} @Nums;

    print $Front;

    In the above code, we modify the parameter by adding a greater than two conditional. Simply put, we want the first value larger than the number 2. The result, of course, is:

      3

    And likewise, we can also use things like AND and OR as well:


    #!/usr/bin/perl

    use List::Util qw(first);

    @Nums=(1,2,3,4);

    $Front = first {$_<5 && $_>3} @Nums;

    print $Front;

    Here, we use the following criteria: less than five, yet greater than three. This returns the number four.

    More Perl Articles
    More By James Payne


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

       

    PERL ARTICLES

    - 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
    - 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





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