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

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

  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



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


use List::Util qw(first);


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


use List::Util qw(first);


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


use List::Util qw(first);


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

print $Front;

This will result in:


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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


Dev Shed Tutorial Topics: