Home arrow Perl Programming arrow Perl Lists: Utilizing List::Util

Perl Lists: Utilizing List::Util

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

In our previous article we discussed, in large part, the split() function and its many uses. As those of you who read that article will recall, the split() function's main purpose is to take a string, break it apart, and return a list full of strings. There were many ways to do this and I think we covered the lion's share of them. Finally, we also went over how to assign a list to another list. And another list. And so forth.

Before we begin talking about the first of our new subroutines, the first{BLOCK}List, let's look at a table describing what each of the seven do:


Subroutine

What It Does

first{BLOCK}List

Used to return the first value in a list. You can also use conditionals to return the first value in the list that matches your criteria.

max(List)

Used to retrieve the largest number in a list.

maxstr(List)

Used to return the largest string value in a list.

min(List)

Used to return the smallest number value in a list.

minstr(List)

Used to return the smallest string value in a list.

shuffle(List)

Used to retrieve the list in an order that is random.

sum(List)

Used to add the numbers in a list and return the value.

Before you begin tackling these examples, take note: you must add the following line to your code when using these subroutines. You add it near the top and you have to include any subroutine you plan on using throughout your program:


Use List::Util qw(first max maxstr);

If you were going to also use shuffle, you would write:


Use List::Util qw(first max maxstr shuffle);

and so forth. This tells the interpreter that you are going to use: first, max, maxstr, and shuffle in your program.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: