Home arrow Perl Programming arrow Page 5 - Perl Lists: More Functions and Operators

A Few Operators - Perl

We left off covering the splice() function, which can be used for adding and removing elements from a list. We also spoke about four other functions: pop(), push(), shift(), and unshift(), which are all similar to the splice() function, just not as flexible. In this article, we'll start out by discussing the splice() function in more detail, and learning how to remove more than one element in a list.

TABLE OF CONTENTS:
  1. Perl Lists: More Functions and Operators
  2. Removing Elements without Storing Them
  3. Using Splice() to Add and Replace
  4. Adding Values to a List with Splice()
  5. A Few Operators
By: James Payne
Rating: starstarstarstarstar / 2
March 31, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You can use some operators on lists in a surprising way. While we don't have time to cover them all here, I figured I would spend this time showcasing some of them for you. For instance, there is the + operator. Let's see what happens when we add a list to itself:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

','Fruit-Punch ','Orange ');

print @KoolAidFlavors + @KoolAidFlavors;

You may or may not (depending on how your brain works) expect this to result in:

  Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange

However, you would be wrong. Instead, the program counts the number of elements in the array and adds them together. Since there are five elements in our list, the result is:

  10

Likewise, if we use the following code:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

','Fruit-Punch ','Orange ');

print @KoolAidFlavors + 2;

We get the result:

  7

Or the number of elements, plus 2.

  You will note that the subtraction operator works in the same way:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

','Fruit-Punch ','Orange ');

print @KoolAidFlavors - 2;

Here the result will be 3, as 5 (the number of elements in the list) -2 is equal to 3.

But what if we use the multiplication symbol (x, not *)? Here, we have a completely different result:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

','Fruit-Punch ','Orange ') x 2;

print @KoolAidFlavors;

Now this will print the array twice. If we had put x3, it would print it three times and so forth. Note that I put the x2 at the end of the list. Had I put it after the print (and used * instead of x), then it would have simply multiplied he number of elements by 2 and returned the number 10. If I had put the x2 after the print command it would have returned 55, for reasons with which I am unfamiliar.

The result of the above code is:

  Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange

Note that the value in @KoolAidFlavors is now all of the above. The code doesn't simply print out the value of the list twice; it stores it twice (or however many times you decide).

Another operator we can use is the "..". This allows us to add sequential values to a list. It's best viewed in code:


#!/usr/bin/perl

@NumberList = (1 .. 20);

print @NumberList;

This will store the values from 1-20 in our list. When we print it out we get the result:

  1234567891011121314151617181920

To further illustrate how this works, in the following code we assign the sequential values the same, but only print out the seventh element (remember that element number begin with 0):


#!/usr/bin/perl

@NumberList = (1 .. 20);

print @NumberList[7];

The result of this code is:

  8

 

This also works with letters:

#!/usr/bin/perl

@Alpha = (a .. h);

print @Alpha;

print "\n\n";

print @Alpha[3];

This will print out:

  abcdefgh

  d

Note that this will not work in reverse; I couldn't type in 10 .. 1 and expect it to return: 10987654321 etc.

Conclusion

We covered a lot of ground in this article. In our next tutorial in this series we will go over the split() function, the List::Util, and maybe, just maybe touch upon those hashes I've been promising. There's only one way to find out though, and that's to drop by as often as possible. So do that.

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: