perl中shift与unshift的用法解析
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');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,and reduce the size of the stack by one. @myNames = ( 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中也一样,功能一样,操作的位置不一样。 ? ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |