Home arrow Perl Programming arrow Page 3 - Perl Lists: A Final Look at List::Util

MinStr(List) Like Them Small - Perl

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.

TABLE OF CONTENTS:
  1. Perl Lists: A Final Look at List::Util
  2. Min(List)...Your Guide to Minimalism
  3. MinStr(List) Like Them Small
  4. Do the Shuffle
By: James Payne
Rating: starstarstarstarstar / 3
April 21, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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



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