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