Home arrow Perl Programming arrow Page 2 - Perl Lists: Utilizing List::Util

First Up On the Block - Perl

In this fifth part of our series on Lists, we will cover the List::Util module, which gives us seven handy-dandy subroutines with which to manipulate our lists. If we have time, we'll also cover multi-dimensional lists in more depth (we briefly covered them in an earlier article).

TABLE OF CONTENTS:
  1. Perl Lists: Utilizing List::Util
  2. First Up On the Block
  3. Making First a Little More Complicated
  4. Max()ing It Out
  5. My String is Bigger Than Yours: The MaxStr(List) Story
By: James Payne
Rating: starstarstarstarstar / 2
April 14, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: