Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





Splice Function


Tutorials Perl

Topic

What is Splice Function?
How to remove or replace a specific array element?



Explanation

Splice function is used to remove specific elements from an array, if required we can replace a list of values in place of removed characters.

Syntax:
    splice ARRAY, OFFSET [, LENGTH [, LIST]];

In the above syntax "ARRAY" is the array element to be spliced, "OFFSET" is the starting value, "LENGTH" specifies the number of characters to be spliced including the "offset", one can also specify the "LIST" of elements to have list "length" parameter is mandatory, but its not viceversa.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    @fruits = ("orange", "guava", "grapes", "cherry", "berry");
    splice @fruits,1,3;
    print "@fruits\n";
Result:
    orange berry

In the above example the we have used the offset of [1] and the length is "3", so the first element "guava" and two more element "grapes", "cherry" are spliced and the rest of array elements are displayed.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    @fruits = ("orange", "guava", "grapes", "cherry", "berry");
    splice @fruits,1,3,apple;
    print "@fruits\n";
Result:
    orange apple berry 

We have used the same example with a list element "apple". To remove / replace a list element the offset, length value is mandatory.








A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting