Welcome to the sixth part of our series on Perl Lists. Here we will cover the remaining four List::Util subroutines: min(list), minstr(list), shuffle(list), and sum(list). We'll learn how to manipulate lists using these subroutine in various intended and unintended ways.
In our previous article we discussed the first{block}List subroutine, which returned the first value in a list. It also could be used to return the first value in a list that met a specific criteria, such as the first value greater than "x" or the first value greater "x" but less than "y," and so forth. We also covered max(List), which finds and returns the maximum numeric value in a list, and maxstr(List), which find the maximum string value in a list. We learned that, where Perl is concerned, "z" is greater in value than "Z," "A," and "a."
Before we begin covering the last of our subroutines, let's take a final look at the List::Util table:
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.
Also remember that before you can use any of the List::Util subs, you must include them near the top of your code, like so:
use List::Util qw(maxstr);
And you must make sure to include whatever subroutines you are going to use in your program in the parameters of qw(). For example:
use List::Util qw(minstr sum min shuffle);
would allow you to use the minstr, sum, min, and shuffle subroutines.