Perl Lists: More Functions and Operators

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.

The last example we discussed in our previous article dealt with removing one element from a list. Here, we will preview briefly the arguments that make up the splice() function, and learn how to add and remove multiple elements in a list with them.

As we stated before, here are the arguments that make up a splice() function: 

  • Push: used to add an item to the right side (or end) of a list

  • Pop: a horrible type of music whose name derives from the word popular. Also, used to remove an item from the right side (or end) of a list

  • Shift: used to remove an item from the left side (or beginning) of a list

  • Unshift: used to add an element to the right side (or beginning) of a list

  • Splice: used to remove and add elements whose location you specify

Removing More than One Element in a List

Removing more than one element in a list is pretty similar to removing just one. All you do is change two things: the number of elements you wish to use, and the variable where you will store the removed elements from a scalar to a list. Here it is in code:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

@bestflavor=splice(@KoolAidFlavors, 1,2);

print @KoolAidFlavors;

print "nn";

print @bestflavor;

Here, we have take two elements from the @KoolAidFlavors list and stored them in @bestflavor. Here is the result of the code:

  Cherry Watermelon

{mospagebreak title=Removing Elements without Storing Them}

You might not wish to store the elements you removed while using splice(). It takes up memory, and can equate to more code and variables floating around than you really want. Here is how you remove elements without storing them:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,2);

print @KoolAidFlavors;

print "nn";

Here we create the @KoolAidFlavors list and add values to it. Next we print out the list, then use splice() to remove the two elements that follow the first element. Finally we print @KoolAidFlavors again, showing that we extracted some data from it. Here is the result:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Fruit-Punch Orange

You can also remove items using a null list:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,2,());

print @KoolAidFlavors;

print "nn";

Which gives us the same result as above.

{mospagebreak title=Using Splice() to Add and Replace}

We can also use splice() to add and replace elements in an array. Let’s say we want to get rid of two flavors from our list, and replace them with two others:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

@rem = (‘Pomegranate ‘, ‘Blueberry ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,2,(@rem));

print @KoolAidFlavors;

print "nn";

This code creates the @KoolAidFlavors list, adds a value to it, and then creates the @rem list, adds values to it, and prints out the value of KoolAidFlavors. Next we use splice() to replace the two elements following the first element with the values in the @rem list. When we run this program, we get the following print-out:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Pomegranate Blueberry Fruit-Punch Orange

Note that we don’t need to use a list to add and replace values in our @KoolAidFlavors list:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,2,(‘Pomegranate ‘,’Blueberry ‘));

print @KoolAidFlavors;

print "nn";

This gives us the same result as above:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Pomegranate Blueberry Fruit-Punch Orange

{mospagebreak title=Adding Values to a List with Splice()}

So far we have seen how to remove items from lists and replace items using the splice() function. In this section we will learn to simply add values. It works in a similar manner, only you change the number for the elements to remove to 0, like so:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,0,(‘Pomegranate ‘,’Blueberry ‘));

print @KoolAidFlavors;

print "nn";

Here, instead of inserting and replacing the values Cherry and Watermelon with Pomegranate and Blueberry, we will append them to the table. In the line: splice(KoolAidFlavors,1,0,(‘Pomegranate ‘,’Blueberry ‘)); we tell the program to insert the two values after the first element. Remember that the 0 tells the program not to replace any elements. If we had written 2,0 then the values would have been added after the second element, and so forth.

The result:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Pomegranate Blueberry Cherry Watermelon Fruit-Punch Orange

And just as we can remove values in our @KoolAidFlavors list by using another list, so too, can we add them:


#!/usr/bin/perl

@KoolAidFlavors = (@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon

‘,’Fruit-Punch ‘,’Orange ‘);

@NewFlavors = (‘Pomegranate ‘, ‘Blueberry ‘);

print @KoolAidFlavors;

print "nn";

splice(@KoolAidFlavors, 1,0,(@NewFlavors));

print @KoolAidFlavors;

print "nn";

The result:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Pomegranate Blueberry Cherry Watermelon Fruit-Punch Orange

It looks the same as before. Which way is best really depends on the situation and your own personal preferences.

{mospagebreak title=A Few Operators}

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 "nn";

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…

Google+ Comments

Google+ Comments