Perl Learning - 4 (pop/push/shift/unshift, foreach, $_)
2012-11-05 16:35
507 查看
pop() & push()
pop() get the last element out of array, and returns that element.
@array=5..9;
$fred=pop(@array); # $fred is 9, @array now is (5, 6, 7, 8)
$barney=pop @array; # $barney is 8, @array now is (5, 6, 7)
pop @array; # @array now is (5, 6)
Retuned value can be throw away, it's ligal in Perl.
If array is empty, pop() do nothing and returns undef.
The ( ) after pop can be skipped, Perl says if skipping something doesn't make difference, then let's skip it.
push() do the oppsite things to pop(), it adds elements to the end of an array.
push(@array,0); # @array now is (5, 6, 0)
push @array,8; # @array now is (5, 6, 0, 8)
push @array,1..10; # 10 more elements
@others=qw/9 0 2 1 0/;
push @array,@others; # 5 more elements
Note the fist parameter of push() and the only parameter of pop() must be an array.
shift() & unshift()
Shift() removes the first element out of an array, unshift() adds elements to the beginning of an array.
@array=qw#dino fred barney#;
$m=shift(@array); # $m is 'dino', @array now is ('fred', 'barney')
$n=shift @array; # $n is 'fred', @array now is ('barney')
shift @array; # @array now is empty ()
$o=shift @array; # @array still empty, $o is undef
unshift(@array, 5); # @array becomes (5)
unshift @array,4; # @array becomes (4, 5)
@others=1..3;
unshift @array,@others; # @array becomes (1, 2, 3, 4, 5)
Array cen be inserted to pire of " ", element will be serarated by space automatically.
@rocks=qw{flintstone slate rubble};
print "quartz @rocks limestone\n"; # prints "quartz flintstone slate rubble limestone"
It will not present spaces before the head or after the end of array, just add space between elements.
foreach() can go through all elements of an array and produce a loop.
foreach $rock (qw/bedrock slate lava/){
print "One rock is $rock.\n";
}
The contral variable $rock get an element every time it loops, until the last element it ends.
The contral variable changes the values of elements when it loops, not only getting a copy.
If there is another variable with same name as the contral variable, when loop starts the varable cannot change.
Until loop ends, the variable recovers it's original value ahead of loop.
If no contral variable is determined, Perl uses $_ as contral variable.
foreach(1..10){
print "I can count to $_!\n";
}
$_="Yabba dabba doo\n";
print; # $_ will be printed
Whenever you cannot find a varable, it could be a $_ working there ^_^
pop() get the last element out of array, and returns that element.
@array=5..9;
$fred=pop(@array); # $fred is 9, @array now is (5, 6, 7, 8)
$barney=pop @array; # $barney is 8, @array now is (5, 6, 7)
pop @array; # @array now is (5, 6)
Retuned value can be throw away, it's ligal in Perl.
If array is empty, pop() do nothing and returns undef.
The ( ) after pop can be skipped, Perl says if skipping something doesn't make difference, then let's skip it.
push() do the oppsite things to pop(), it adds elements to the end of an array.
push(@array,0); # @array now is (5, 6, 0)
push @array,8; # @array now is (5, 6, 0, 8)
push @array,1..10; # 10 more elements
@others=qw/9 0 2 1 0/;
push @array,@others; # 5 more elements
Note the fist parameter of push() and the only parameter of pop() must be an array.
shift() & unshift()
Shift() removes the first element out of an array, unshift() adds elements to the beginning of an array.
@array=qw#dino fred barney#;
$m=shift(@array); # $m is 'dino', @array now is ('fred', 'barney')
$n=shift @array; # $n is 'fred', @array now is ('barney')
shift @array; # @array now is empty ()
$o=shift @array; # @array still empty, $o is undef
unshift(@array, 5); # @array becomes (5)
unshift @array,4; # @array becomes (4, 5)
@others=1..3;
unshift @array,@others; # @array becomes (1, 2, 3, 4, 5)
Array cen be inserted to pire of " ", element will be serarated by space automatically.
@rocks=qw{flintstone slate rubble};
print "quartz @rocks limestone\n"; # prints "quartz flintstone slate rubble limestone"
It will not present spaces before the head or after the end of array, just add space between elements.
foreach() can go through all elements of an array and produce a loop.
foreach $rock (qw/bedrock slate lava/){
print "One rock is $rock.\n";
}
The contral variable $rock get an element every time it loops, until the last element it ends.
The contral variable changes the values of elements when it loops, not only getting a copy.
If there is another variable with same name as the contral variable, when loop starts the varable cannot change.
Until loop ends, the variable recovers it's original value ahead of loop.
If no contral variable is determined, Perl uses $_ as contral variable.
foreach(1..10){
print "I can count to $_!\n";
}
$_="Yabba dabba doo\n";
print; # $_ will be printed
Whenever you cannot find a varable, it could be a $_ working there ^_^
相关文章推荐
- JS数组方法总结shift()、unshift()、pop()、push()、concat()、splice()、filter()、map()、some()、every()、forEach()等方法
- js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join
- Js:数组的各种方法范例pop,push,unshift,splice,shift
- 数组的各种方法范例pop,push,unshift,splice,shift
- array push pop shift unshift +javascript
- Javascript的shift()和push(),unshift()和pop()方法简介
- js队列方法使用push(xx)结合shift()实现或者使用pop()结合unshift()实现
- Js数组中的pop ,push,unshift,splice,shift方法
- 转:js数组操作大全(pop,push,unshift,splice,shift方法)
- [乐意黎转载]JS中push()和pop()以及unshift()和shift()的用法小结
- js 数组操作之pop,push,unshift,splice,shift
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- 数组的push()、pop()、shift()和unshift()方法
- JavaScript数组中shift()和push(),unshift()和pop()操作
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- Javascript的shift()和push(),unshift()和pop()方法简介
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- js数组的常见操作( push、pop、unshift、shift、splice、concat、 join)的用法
- JS 数组array方法push, pop, unshift, shift, slice,splice,contact, join, sort
- js数组操作大全(pop,push,unshift,splice,shift方法)