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

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.

{mospagebreak title=First Up On the Block}

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.

{mospagebreak title=Making First a Little More Complicated}

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


#!/usr/bin/perl

use List::Util qw(first);

@Nums=(1,2,3,4);

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


#!/usr/bin/perl

use List::Util qw(first);

@Nums=(1,2,3,4);

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


#!/usr/bin/perl

use List::Util qw(first);

@Nums=(1..50);

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

print $Front;

This will result in:

  25

{mospagebreak title=Max()ing It Out}

The Max function is used to return the maximum value in a list. It’s that simple. Here it is at work:


#!/usr/bin/perl

use List::Util qw(max);

@Nums=(1,900,50,22);

$High=max(@Nums);

print $High;

This goes through our list @Nums, looks for the highest number, and places it in the $high variable. Here is the obvious result:

  900

As before, this bad boy can be used in conjunction with operators, statements, etc. Here it is with an if…else:


#!/usr/bin/perl

use List::Util qw(max);

@Nums=(1,900,50,22);

$high=max(@Nums);

if($high > 25) {print "The number $high is greater than 25!"}

else {print "The value $high is less than 25!"};

This will print out:

  The value 900 is greater than 25!

We can also perform calculations off of our subroutine. Here is a simple addition:


#!/usr/bin/perl

use List::Util qw(max);

@Nums=(1,900,50,22);

$high=max(@Nums) + 1;

print $high;

Giving us:

  901

Here is a final example showcasing how you can compare the highest values of two lists:


#!/usr/bin/perl

use List::Util qw(max);

@Nums=(1,900,50,22);

@Nums2=(90..898);

$High=max(@Nums);

$Less=max(@Nums2);

if($High > $Less) {$Less=$High};

print $High . "n";

print $Less;

This code creates two arrays. We fill one with some values and we use a sequential operator to fill the other. Next, we use max to find the maximum value in each and store them in their respective variables. Finally, we use an if statement to determine whether $High is greater than $Less. If so, then the value in $Less is given the same value as the variable $High. We then print out the result of both variables:

  900

  900

You will note that we only used numbers in the previous example. In order to find the highest string value, we use our good buddy and max(List)’s cousin, maxstr(List).

{mospagebreak title=My String is Bigger Than Yours: The MaxStr(List) Story}

To compare string values and extract the largest, we use maxstr. Below are some examples:


#!/usr/bin/perl

use List::Util qw(maxstr);

@Letters=(‘A’,'B’,'C’,'D’);

$Big=maxstr(@Letters);

print $Big;

In this simple example, we compare the values of A, B, C, D. Which one is larger?

  D

Pretty simple right? What about if we try something like this:


#!/usr/bin/perl

use List::Util qw(maxstr);

@Letters=(‘A’,'a’,'B’,'b’,'C’,'c’,'D’,'d’);

$Big=maxstr(@Letters);

print $Big;

Guess which letter has the highest value? The result:

  d

Lowercase letters in Perl have a higher value than uppercase. What about special characters?


#!/usr/bin/perl

use List::Util qw(maxstr);

@Letters=(‘A’,'a’,'B’,'b’,'C’,'c’,'D’,'d’,'!’,'@’,'#’,'$’);

$Big=maxstr(@Letters);

print $Big;

Again, "d" wins the day.

You will note, of course, that a string can be a whole word and even numbers. Here we compare those:


#!/usr/bin/perl

use List::Util qw(maxstr);

@Letters=(‘Apple’,'Pie’,’999′,’@#$%!’);

$Big=maxstr(@Letters);

print $Big;

The largest string value here is:

  Pie

Lastly, consider this bizarre code, in which we compare the values a-z, A-Z, 1-100, and a bunch of shift characters:


#!/usr/bin/perl

use List::Util qw(maxstr);

@Letters=(‘A’..’Z',’a’..’z',’1′..’100′,’!',’@',’#',’$',’%',’&’,'+’);

$Big=maxstr(@Letters);

print $Big;

And the winner is….<insert drum roll here>:


  z

Conclusion

Well as you can see, we barely scraped the surface in this article. There are four more subroutines left to go, which we should be able to cover in our next tutorial. The min() and minstr() work in a similar way to our max() and maxstr() subroutines. We’ll also go over the shuffle() and sum(). So check back soon.

Till then…

Google+ Comments

Google+ Comments