Perl Programming Perl Lists: More Functions and Operators |
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:
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 "\n\n"; @bestflavor=splice(@KoolAidFlavors, 1,2); print @KoolAidFlavors; print "\n\n"; 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
blog comments powered by Disqus |