加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl中shift与unshift的用法解析

发布时间:2020-12-16 00:10:22 所属栏目:大数据 来源:网络整理
导读: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 fi

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,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','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中也一样,功能一样,操作的位置不一样。 ? ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读