读 《C程序员精通Perl》http://book.douban.com/subject/1232075/ 3.3节 笔记
#!/usr/bin/perl
use strict;
use warnings;
my @array = ();
while (1) {
print "input a number or <enter> to finish: ";
my $a=<STDIN>;
chomp ($a);
if ($a eq "") {
last;
}
push(@array, $a);
}
print "array=@array\n\n";
my @result = splice(@array, 6);
print "after splice1 array=@array\n";
print "result = @result\n\n";
@result = splice(@array, 3, 2);
print "after splice2 array=@array\n";
print "result = @result\n\n";
@result = splice(@array, 3, 0, "hello", "calf_man");
print "after splice3 array=@array\n";
print "result = @result\n\n";
@result = splice(@array, 3, 1, "wonderful", "honey", "cute");
print "after splice4 array=@array\n";
print "result = @result\n\n";
运行结果:
[root@localhost perl_practice]# ./splice.pl
input a number or <enter> to finish: 1
input a number or <enter> to finish: 2
input a number or <enter> to finish: 3
input a number or <enter> to finish: 4
input a number or <enter> to finish: 5
input a number or <enter> to finish: 6
input a number or <enter> to finish: 7
input a number or <enter> to finish: 8
input a number or <enter> to finish: 9
input a number or <enter> to finish:
array=1 2 3 4 5 6 7 8 9
after splice1 array=1 2 3 4 5 6
result = 7 8 9
after splice2 array=1 2 3 6
result = 4 5
after splice3 array=1 2 3 hello calf_man 6
result =
after splice4 array=1 2 3 wonderful honey cute calf_man 6
result = hello
[root@localhost perl_practice]#