Home arrow Perl Programming arrow Page 4 - Perl Lists: More Functions and Operators

Adding Values to a List with Splice() - Perl

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.

TABLE OF CONTENTS:
  1. Perl Lists: More Functions and Operators
  2. Removing Elements without Storing Them
  3. Using Splice() to Add and Replace
  4. Adding Values to a List with Splice()
  5. A Few Operators
By: James Payne
Rating: starstarstarstarstar / 2
March 31, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

splice(@KoolAidFlavors, 1,0,('Pomegranate ','Blueberry '));

print @KoolAidFlavors;

print "\n\n";

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

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

print @KoolAidFlavors;

print "\n\n";

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.



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