Splice Function

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:programfilesperlbinperl
print "content-type: text/htmlnn";
@fruits = ("orange", "guava", "grapes", "cherry", "berry");
splice @fruits,1,3;
print "@fruitsn";
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:programfilesperlbinperl
print "content-type: text/htmlnn";
@fruits = ("orange", "guava", "grapes", "cherry", "berry");
splice @fruits,1,3,apple;
print "@fruitsn";
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.

Ask Questions

Ask Question