SunQuest
 
       Perl
  Home arrow Perl arrow Page 3 - 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 
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

    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 - Making First a Little More Complicated


    (Page 3 of 5 )

    Here is some code that showcases some of the ways you can modify the parameters of the first{Block}List subroutine:


    #!/usr/bin/perl

    use List::Util qw(first);

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

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

    $Examp = first {$_} @Nums;

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

    $ExampThree = first {$_>1 and $_<2 or $_>1 && $_<3} @Nums;

    print $Front . " ";

    print $Examp . " ";

    print $ExampTwo . " ";

    print $ExampThree . " ";

    This results in:

      4 1 3 2

    We can use conditional statements with our subroutine as well:


    #!/usr/bin/perl

    use List::Util qw(first);

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

    $Front=first{$_} @Nums;

    if($Front==1) {print "The value equals 1!"};

    $Diff=first{$_>3} @Nums;

    if($Diff==1) {print "The value equals 1!"}

    else {print "The value is equal to $Diff!"};

    This code returns: The value equals 1! The value is equal to 4!

    And lastly, we can also use a sequential operator to assign the value to the array and use a conditional to extract a number from it:


    #!/usr/bin/perl

    use List::Util qw(first);

    @Nums=(1..50);

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

    print $Front;

    This will result in:

      25

    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 5 hosted by Hostway