Perl Lists: A Final Look at List::Util

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.

{mospagebreak title=Min(List)…Your Guide to Minimalism}

Min(List) is very similar to max(List) in its usage. The only difference is that it returns the minimum numeric value in a list. Here we have it in code:


#!/usr/bin/perl

use List::Util qw(min);

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

$Low=min(@Nums);

print $Low;

Here, we create a list named ‘@Nums’, in which we store several numeric values. Next, we use min() to scan the list and find the lowest value, and store it in the $Low variable. Finally, we print the value out, resulting in:

  1

As with our other subroutines, we can use min() in conjunction with operators, statements, and the like. Here it is with an if…else statement:


#!/usr/bin/perl

use List::Util qw(min);

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

$Low=min(@Nums);

if($Low > 2) {print "The number $Low is greater than 2!"}

else {print "The value $Low is less than 2!"};

Again, we call our subroutine, then create the @Nums list, filling it with values. We then use min() to parse out the lowest value and store it in $Low. Lastly, we create an if…else statement that says if the value of $Low is greater than two, print out a statement. If the value is less than two, print out another statement. You will note of course that if the value were two, nothing would have happened. Here is the result:

  The value 1 is less than 2!

We can perform some mathematics on our subroutine as well:


#!/usr/bin/perl

use List::Util qw(min);

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

$Low=min(@Nums) + 1 * 25 +6 /9 -2;

print $Low;

Here we take the value of $Low and beat it to death with a bunch of math that I randomly selected, yet which oddly enough put us almost back at the number 25:

  24.66666666666667

Give or take a six…

Finally, here is how we can compare the lowest values from two separate lists and have something occur from the result:


#!/usr/bin/perl

use List::Util qw(min);

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

@Nums2=(90..898);

$Low=min(@Nums);

$Low2=min(@Nums2);

if($Low > $Low2) {print "$Low is greater than $Low2"}

else {print "$Low is less than $Low2"};

print "n" .$Low . "n";

print $Low2;

Here, this code creates two lists, @Nums and @Nums2. It assigns the first list some numeric values, and then uses a sequential operator to add the values 90-898 to @Nums2. Next, it uses min() to extract the lowest numeric value from @Nums and assign it to $Low. It then uses min() again to assign the lowest numeric value in @Nums2 to the variable $Low2. Finally, it runs an if…else statement that states if $Low is greater than $Low2, print something. Otherwise it will print something else. Note that if $Low and $Low2 are equal, nothing will happen.

Here is the result:

  1 is less than 90

  1

  90

{mospagebreak title=MinStr(List) Like Them Small}

This subroutine works like min(), only for strings. It will find the smallest valued string in a list, and return it. Here are some examples of its use:


#!/usr/bin/perl

use List::Util qw(minstr);

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

$Small=minstr(@Letters);

print $Small;

In the above example, we call the minstr() sub near the top, then create a list, @Letters, and give it some values. Next, we use minstr() to extract the smallest value in the list and store it in our $Small variable. Finally, we print out the result:

  A

As you may recall from our maxstr() examples in the previous article, the values ‘A’ and ‘a’ are not equal. Consider this code:


#!/usr/bin/perl

use List::Util qw(minstr);

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

$Small=minstr(@Letters);

print $Small;

Which value will be the lowest here? If you guessed "A," then you are correct. Always remember that uppercase letters in Perl are worth less than lowercase values. Let’s see how they fare against shifted characters:



#!/usr/bin/perl

use List::Util qw(minstr);

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

$Small=minstr(@Letters);

print $Small;

In this example, the smallest result is the exclamation point(!).

Next we will look at whole strings and see how they compare:


#!/usr/bin/perl

use List::Util qw(minstr);

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

$Small=minstr(@Letters);

print $Small;

In this situation, ‘999’ is the smallest value.

Finally, let’s test a-z, A-Z, 1-100, and some more shift characters to see which one has the least value:


#!/usr/bin/perl

use List::Util qw(minstr);

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

$Small=minstr(@Letters);

print $Small;

Again, our smallest value is the exclamation point(!).

{mospagebreak title=Do the Shuffle}

Shuffle is a pretty simple function. It takes a list and returns it in a random order. That’s it. Here it is in code:


#!/usr/bin/perl

use List::Util qw(shuffle);

@nums = shuffle 0..10;

print @nums;

Here we create a list (@Nums) of random numbers by using shuffle. The numbers range from 1-10 and their order will vary each time you run this program. Here is my first result:

  014329685107

Another way of sorting with shuffle is by using a predefined array:


#!/usr/bin/perl

use List::Util qw(shuffle);

@Nums = (1,2,3,4,5,6,7,8);

@Weird=shuffle(@Nums);

print @Weird;

Which once again gives us a randomly shuffled list. My first result was:

  25871643

Sum()ming It Up

Our final subroutine is sum(), which is used to sum up the numeric values in a list. Here are some samples:


#!/usr/bin/perl

use List::Util qw(sum);

@Nums = (1,2,3,4,5,6,7,8);

@Weird=sum(@Nums);

print @Weird;

Here, the result is:

  36

Or 8+7+6+5+4+3+2+1

We can also assign some wacky values using expressions in our list, and then sum up the total:


#!/usr/bin/perl

use List::Util qw(sum);

@Nums = (1+1,2+2,3*3,4*8,5*7,6*6,7+4,8+2);

@Weird=sum(@Nums);

print @Weird;

Here we are given the sum:

  139

Lastly, we can find the sum of more than one list by using the following technique:


#!/usr/bin/perl

use List::Util qw(sum);

@Nums = (1+1,2+2,3*3,4*8,5*7,6*6,7+4,8+2);

@Nums2= (1,2,3,4,5,6,7,8);

@Weird=sum(@Nums,@Nums2);

print @Weird;

This takes the two arrays, adds them together, and gives us the sum:

  175

Conclusion

And so we come to the end of our discussion of the List::Util subroutines. In our next tutorial, we will finally discuss the long anticipated Hashes, and perhaps, if not in the next article then the one after that, the Multidimensional list. There’s a lot to cover, so be sure to drop by often.

Till then…

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye