Home arrow Perl Programming arrow Page 4 - Perl Lists: More on Manipulation

Splice...It Sounds Like a New Citrus Soda, But It's Not - Perl

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.

TABLE OF CONTENTS:
  1. Perl Lists: More on Manipulation
  2. Pop() Goes Your Data
  3. Unshift My Heart
  4. Splice...It Sounds Like a New Citrus Soda, But It's Not
By: James Payne
Rating: starstarstarstarstar / 4
March 24, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 "\n\n";

$someFlavor=shift(@KoolAidFlavors);

$bestflavor=shift(@KoolAidFlavors);

unshift(@KoolAidFlavors, $someFlavor);

print @KoolAidFlavors;

print "\n\n";

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 "\n\n";

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

print @KoolAidFlavors;

print "\n\n";

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



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