Home arrow Perl Programming arrow Page 5 - Perl Lists: Utilizing List::Util

My String is Bigger Than Yours: The MaxStr(List) Story - Perl

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

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



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