perl中shift与unshift的用法解析
2013-08-07 14:50
495 查看
Perl array shift()&&unshift() function - Quick Tutorial
SHIFT$ITEM = shift(@ARRAY); Perl's shift() function is used to remove and return the first element from an array, which reduces the number of elements by one. The first element in the array is the one with the lowest index. It's easy to confuse this function with pop(), which removes the last element from an array. @myNames = ('Larry', 'Curly', 'Moe');
$oneName = shift(@myNames); If you think of an array as a row of numbered boxes, going from left to right, it would be the element on the far left. The shift() function would cut the element off the left side of the array, return it, and reduce the elements by one. In the examples, the value of $oneName becomes 'Larry', the first element, and @myNames is shortened to ('Curly', 'Moe'). The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The shift() function would shift the element off the top of the stack, return it, and reduce the size of the stack by one. @myNames = (
'Larry',
'Curly',
'Moe'
);
$oneName = shift(@myNames);
UNSHIFT
$TOTAL = unshift(@ARRAY, VALUES); Perl's unshift() function is used to add a value or values onto the beginning of an array (prepend), which increases the number of elements. The new values then become the first elements in the array. It returns the new total number of elements in the array. It's easy to confuse this function with push(), which adds elements to the end of an array. @myNames = ('Curly', 'Moe');
unshift(@myNames, 'Larry'); Picture a row of numbered boxes, going from left to right. The unshift() function would add the new value or values on to the left side of the array, and increase the elements. In the examples, the value of @myNames becomes ('Larry', 'Curly', 'Moe'). The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The unshift() function would add the value to the top of the stack, and increase the overall size of the stack. @myNames = (
'Curly',
'Moe'
);
unshift(@myNames, 'Larry'); You can unshift() multiple values onto the array directly: @myNames = ('Moe', 'Shemp');
unshift(@myNames, ('Larry', 'Curly')); Or by unshift()-ing an array: @myNames = ('Moe', 'Shemp');
@moreNames = ('Larry', 'Curly');
unshift(@myNames, @moreNames); shift 将数组最左边的数据移出并返回,返回一个元素,减少数组元素 (shift()与pop()功能差别:pop()为移出数据最右边的数据,或说stack中的top元素)
unshift 将元素或数组的值,加到所指数组的首部,返回新的数组,增加数组的元素 (unshift与push()功能相似,有差别:push()为在数组尾部加值,或stack中的top位置)
eg.$var=shift(@array)@morearray=unshift(@arrary,'lucy')
shift(首)---pop(尾) unshift(首)---push(尾) stack中也一样,功能一样,操作的位置不一样。 本文出自 “奔跑的羚羊” 博客,请务必保留此出处http://heshw.blog.51cto.com/5891747/1266010
相关文章推荐
- js中push(),pop(),unshift(),shift()的用法小结
- perl中shift 和unshift 操作
- js中push(),pop(),unshift(),shift()的用法
- js中push(),pop(),unshift(),shift()的用法小结
- [乐意黎转载]JS中push()和pop()以及unshift()和shift()的用法小结
- perl pop push shift unshift实例介绍
- Perl学习(十三)——数组操作总结 push pop shift unshift
- 解析Perl 的shift / ?通配符
- perl学习之:shift/unshift
- perl 变量 $/ 的用法解析 上下文为行模式时,$/ 定义以什么来区分行
- js中push(),pop(),unshift(),shift()的用法小结
- perl 变量 $/ 的用法解析:上下文为行模式时,$/ 定义以什么来区分行
- perl中shift 和unshift 操作
- 3 Perl - 列表赋值 - shift 和unshift 操作
- js shift unshift pop push用法
- js中push(),pop(),unshift(),shift()的用法小结
- Perl数组: shift, unshift, push, pop
- Perl命令行用法解析
- Perl命令行用法解析
- perl中shift 和unshift 操作