Perl Lists: More on Manipulation

In our last article we talked about lists, going somewhat in-depth on their capabilities. We talked about creating them, printing from them, and even used a slicing technique to add and remove from our lists. In this article we will start off looking at more traditional ways to add and remove from a list, and move on from there.

How To Add and Remove Elements in a List the Old-Fashioned Way

There are several functions in Perl that allow you to add or remove elements from a list. In this next section, we will cover them all. Here they are in a bulleted list to get your lips drooling:

  • 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 left side (or beginning) of a list.

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

Ahh…Push It…Push it Real Good!

I’ll have to admit it: I was in fifth grade and I had formed a new rap band with the menacing name 2Bad4Ya. My teacher at the time, one Mr. Bonner (whose name we made fun of all the time) also knew some people in the industry. So I decided to "borrow" the music to that piece of poetry by Salt N Peppa and add my own lyrics to it. I then proceeded to play it for my teacher, and fortunately I was too dumb at the time to be as embarrassed as I should have been when he chuckled, played with his goatee, ran a hand through his jerri-curls and said, "Uhhh yeah, dat’s nice."

As we said in our table above, the push() function can be used to add an item to the right side of a list. But enough talking; here it is in code:


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘, ‘TheNutcracker ‘, ‘Glutious-Minimus ‘);

@new=(‘Max Fightmaster ‘);

push(@gladiators, @new);

print @gladiators;

This code creates two lists, the gladiators and new arrays. We assign values to each, and then use the push() function to add the values in our @new list to our @gladiators list, which of course results in:

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus Max Fightmaster

But what if that temp you hired added the wrong name? Or typoed it? Or the person quit? Never fear (unless Hillary Clinton becomes president). To remove that record, or any record, from the right hand side, simply use the pop() function.

{mospagebreak title=Pop() Goes Your Data}

What else is there to say about the pop() function? Nothing, that’s what. Here is the code:


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘, ‘TheNutcracker ‘, ‘Glutious-Minimus ‘);

@new=(‘Max Fightmaster ‘);

push(@gladiators, @new);

print @gladiators;

print "nn";

$deleted=pop(@gladiators);

print @gladiators;

print "nn";

print $deleted;

This code again creates the two lists and adds the values to them, then uses push() to add the data from @new to @gladiators. We print out the value of @gladiators to show it worked, and then create a new variable named $deleted, using the pop() function to remove the last element in the @gladiators array and store it in $deleted. Finally, we print out the values of both the @gladiator list, and the $deleted variable, resulting in (takes a deep breath):

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus Max Fightmaster

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus

  Max Fightmaster

You will note that when we remove data from a list in this manner it gets stored in a variable, not a list. Likewise, you cannot, with this method, deduct data from a list using another list; again, this is because you are simply removing the end element, and not "elements."

{mospagebreak title=Unshift My Heart}

The unshift() function is similar to push except that it adds an item to the front or left side of your list. For this example, let’s start with a blank list and fill it with data (you can do the same using push):


#!/usr/bin/perl

@KoolAidFlavors;

unshift(@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon ‘,’Fruit-Punch

‘,’Orange ‘);

print @KoolAidFlavors;

The result?

  Grape Cherry Watermelon Fruit-Punch Orange

On the Night Shift

The shift() function works like the pop() function except it removes the element from the left side of the list. Want to see the code? Too bad:


#!/usr/bin/perl

@KoolAidFlavors;

unshift(@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon ‘,’Fruit-Punch

‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

$grossFlavors=shift(@KoolAidFlavors);

print @KoolAidFlavors;

print "nn";

print $grossFlavors;

Again, we create the @KoolAidFlavors as a null list, then add values to it using the unshift() function. Next we print out the value, then create a new variable named $grossFlavors, which will hold the gross flavors from the list. Of course, the gross flavor must also be the first element for this to work. We use shift() to grab the first element in the list, then print out both the updated list and the variable, resulting in:

  Grape Cherry Watermelon Fruit-Punch Orange

  Cherry Watermelon Fruit-Punch Orange

  Grape

Tada!

{mospagebreak title=Splice…It Sounds Like a New Citrus Soda, But It’s Not}

So far we have been confined to adding and removing elements from a list from the left and right hand sides. Which leaves us up nerd creek without a paddle if we need to remove a value in the middle. That isn’t to say that you can’t remove a value from the center; you can with some ridiculous code. Oh, you would like to see that, would you? So would my minimum word count:


#!/usr/bin/perl

@KoolAidFlavors;

unshift(@KoolAidFlavors, ‘Grape ‘,’Cherry ‘,’Watermelon ‘,’Fruit-Punch

‘,’Orange ‘);

print @KoolAidFlavors;

print "nn";

$someFlavor=shift(@KoolAidFlavors);

$bestflavor=shift(@KoolAidFlavors);

unshift(@KoolAidFlavors, $someFlavor);

print @KoolAidFlavors;

print "nn";

print $bestflavor;

This convoluted code is used to retrieve what I think is the best flavor from the @KoolAidFlavors list, which of course is Cherry. Since the functions we have covered so far only allow us to grab or add from the left or right hand side, we have to get tricky. We do this by creating two new variables and using shift to take out the first element, then use shift again to remove "Cherry," which now becomes the first element. We then use unshift to put "Grape" back into the @KoolAidFlavors list via the $someflavor variable that holds it. Finally, we print out the values of the modified list, and the $bestflavor variable.

A little confusing right? Well fortunately we don’t have to go so crazy to perform what should be, and is, a simple task. Here is how we accomplish the same madness with our friend the splice() function:


#!/usr/bin/perl

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

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

print @KoolAidFlavors;

print "nn";

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

print @KoolAidFlavors;

print "nn";

print $bestflavor;

This will give us the same result as above:

  Grape Cherry Watermelon Fruit-Punch Orange

  Grape Watermelon Fruit-Punch Orange

  Cherry

In the above example you will note that the splice() function has several arguments. They are (in order):


  • The list you wish to modify.

  • The element number where the function should start replacing (if you tell it 1, it will replace the element following that, or 2…if you type 3, element 4 will be the first removed, and so forth).

  • How many elements you want removed.

  • Which list of elements you would like to insert.

Some of these arguments are optional (well, technically they all are) and they can be used in several different ways. But we will get to that in a bit.

In our example above, we used the following line: $bestflavor=splice(@KoolAidFlavors, 1,1). You will note that this use of the function does not have 4 arguments. It has the list that we are going to modify, the position, then the number of elements to extract. The result of this line is that it looks in our @KoolAidFlavors list, sees "Grape" as the first value, and knows to start taking elements after that. It sees that it should only take one element, so it takes Cherry and stores it in the $bestflavor variable.

Be aware that if we had told it to take 2 elements, it would have stored Cherry and Watermelon in our variable. And further, since there would be two values, we would have needed to make $bestflavor into @bestflavor.

Well that is all the time we have for this article. In our next tutorial we will continue our discussion on lists in Perl, and perhaps get to hashes as well.

Till then…

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

chat sex hikayeleri Ensest hikaye