Thursday, November 3, 2011

To delete an element in Array using splice

Splice functions are used to delete / add / replace elements in an existing array:

Below example we see how to  delete elements in array

$ cat splice.pl
#!/usr/bin/perl
@numbers =("one","two","three","four","five","six","seven","eight");
splice(numbers,1,2);
print "array values in Numbers are :\n @numbers\n";
$


You'll see three arguments inside the splice(numbers,1,2); of the splice function above.
The first one is just the name of the array you want to splice.
The second is the list number of the element where you wish to start the splice (starts counting at zero).
The third is the number of elements you wish to splice.

In this case, we just want to splice two elements.
 The code above deletes the element at list number 1 and 2, which is "two","three"

So result should be below

$ ./splice.pl
array values in Numbers are :
 one four five six seven eight
$

No comments:

Post a Comment