From: http://blog.chinaunix.net/u/9861/showart_727153.html
Usage of Perl function Splice:
Splice has four usage options:
1. splicearray, offset, length, list
2. splicearray, offset, Length
3. splicearray, offset
4. splicearray
Description: removes the elements specified by offset and length in the array, and replaces the elements in the list. In the context of the list, the function returns the overflow array element. In the scalar context, return the last overflow element. If no element is removed, return UNDEF.
NOTE: If offset is a negative value, it starts from the end of the array. If length is omitted (in the third case), the elements from offset to the end are removed. If length is negative, all elements from offset to tail are removed except the length element at the end of the array. If both offset and length are empty, remove all elements. If offset is out of bounds, Perl will prompt a warning and insert list or null at the end of the array.
1. splicearray, offset, length, list
#!/usr/bin/perl -w use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,1,2,@tmp); foreach (@rocks){ print$_."##"; } print "\n"; |
[Root @ localhost ~] # Perl refs. pl
Talc ## hell ## oworld ## obsidian ##
2. splicearray, offset, Length
#!/usr/bin/perl -w use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,1,2); foreach (@rocks){ print$_."##"; } print "\n"; |
[Root @ localhost ~] # Perlrefs. pl
Talc ##
3. Splice array, offset
#!/usr/bin/perl -w use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks,2); foreach (@rocks){ print$_."##"; } print "\n"; |
[Root @ localhost ~] # Perlrefs. pl
Talc # quartz ##
4. Splice Array
#!/usr/bin/perl -w use strict; my @rocks = qw(talc quartz jadeobsidian); my @tmp = qw(hell oworld); splice(@rocks); foreach (@rocks){ print$_."##"; } print "\n"; |
[Root @ localhost ~] # Perlrefs. pl
Print Blank