【Perl读书笔记】数组操作push、pop、shift和unshift
2012-12-28 13:42
573 查看
读 《C程序员精通Perl》http://book.douban.com/subject/1232075/ 3.2节 笔记
运行结果:
[root@localhost perl_practice]# ./read2.pl
input a number or <enter> to finish: 1.^H
Add 1 to array
input a number or <enter> to finish: 2
Add 2 to array
input a number or <enter> to finish: 3
Add 3 to array
input a number or <enter> to finish: 4
Add 4 to array
input a number or <enter> to finish: 5
Add 5 to array
input a number or <enter> to finish: 6
Add 6 to array
input a number or <enter> to finish:
array=1 2 3 4 5 6
pop_number = 6
after pop array=1 2 3 4 5
shift_number = 1.
after shift array=2 3 4 5
after unshift array=100 2 3 4 5
[root@localhost perl_practice]#
数组赋值:
#!/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";
@array[0, $#array] = (1000, 2000);
print "1 array=@array\n";
@array[0..$#array] = ((9) x ($#array+1));
print "2 array=@array\n";
运行结果:
[root@localhost perl_practice]# ./read3.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:
array=1 2 3 4 5
1 array=1000 2 3 4 2000
2 array=9 9 9 9 9
[root@localhost perl_practice]#
#!/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; } print "Add $a to array\n"; #@array=(@array, $a); push(@array, $a); } print "\narray=@array\n\n"; my $pop_number = pop(@array); print "pop_number = $pop_number\n"; print "after pop array=@array\n\n"; my $shift_number = shift @array; print "shift_number = $shift_number\n"; print "after shift array=@array\n\n"; unshift(@array, 100); print "after unshift array=@array\n";
运行结果:
[root@localhost perl_practice]# ./read2.pl
input a number or <enter> to finish: 1.^H
Add 1 to array
input a number or <enter> to finish: 2
Add 2 to array
input a number or <enter> to finish: 3
Add 3 to array
input a number or <enter> to finish: 4
Add 4 to array
input a number or <enter> to finish: 5
Add 5 to array
input a number or <enter> to finish: 6
Add 6 to array
input a number or <enter> to finish:
array=1 2 3 4 5 6
pop_number = 6
after pop array=1 2 3 4 5
shift_number = 1.
after shift array=2 3 4 5
after unshift array=100 2 3 4 5
[root@localhost perl_practice]#
数组赋值:
#!/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";
@array[0, $#array] = (1000, 2000);
print "1 array=@array\n";
@array[0..$#array] = ((9) x ($#array+1));
print "2 array=@array\n";
运行结果:
[root@localhost perl_practice]# ./read3.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:
array=1 2 3 4 5
1 array=1000 2 3 4 2000
2 array=9 9 9 9 9
[root@localhost perl_practice]#
相关文章推荐
- JS数组操作示意图(shift,unshift,pop,push)
- js 数组操作之pop,push,unshift,splice,shift
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- ActionScript中数组操作(一):push(pop)与unshift(shift)
- Js数组的操作push,pop,shift,unshift等方法
- js 数组操作之pop,push,unshift,splice,shift
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- js中 数组的操作 push(),pop(),shift(),unshift() 简介
- 转:js数组操作大全(pop,push,unshift,splice,shift方法)
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- js数组操作(push,pop,shift,unshift,slice,splice,concat,sort)
- js数组操作大全(pop,push,unshift,splice,shift方法)
- JavaScript数组中shift()和push(),unshift()和pop()操作
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- js数组操作大全(pop,push,unshift,splice,shift方法)
- 数组的操作push,pop,shift,unshift
- js数组操作大全(pop,push,unshift,splice,shift方法)
- 操作Perl数组: shift, unshift, push, pop
- js数组的常见操作( push、pop、unshift、shift、splice、concat、 join)的用法