Perl Programming Page 4 - Perl Lists: More on Manipulation |
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):
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...
blog comments powered by Disqus |
|
|
|
|
|
|
|