perl push函数_Perl Array Push()函数

perl push函数

The Perl push() function is used to push a value or values onto the end of an array, which increases the number of elements. The new values then become the last elements in the array. It returns the new total number of elements in the array. It's easy to confuse this function with the unshift() function, which adds elements to the beginning of an array. Here's an example of the Perl push() function:

Perl push()函数用于将一个或多个值压入数组的末尾,这会增加元素的数量。 然后,新值成为最后一个元素   在数组中。 它返回数组中新的元素总数。 容易将此函数与unshift()函数混淆,后者将元素添加到开头   数组 这是Perl push()函数的示例:


@myNames = ('Larry', 'Curly');
push @myNames, 'Moe';
print "@myNames\n";

When this code is executed, it delivers:

执行此代码后,它将提供:

拉里·柯莉·莫 ( Larry Curly Moe )

Picture a row of numbered boxes, going from left to right. The push() function pushes the new value or values onto the right side of the array and increases the elements. 

为从左到右的行编号的框图片。 push()函数将一个或多个新值推到数组的右侧,并增加元素。

The array can also be thought of as a stack. Picture a stack of numbered boxes, starting with 0 at the top and increasing as it goes down. The push() function pushes the value onto the bottom of the stack and increases the elements, like this:

数组也可以视为堆栈。 想象一堆编号的框,从顶部的0开始,随着向下的增加而增加。 push()函数将值压入堆栈的底部并增加元素,如下所示:


@myNames = (
<'Larry',
'Curly'
);
push @myNames, 'Moe';

You can also push multiple values onto the array directly ...

您也可以将多个值直接推入数组...


@myNames = ('Larry', 'Curly');
push @myNames, ('Moe', 'Shemp');

... or by pushing on an array:

...或通过推入数组:


@myNames = ('Larry', 'Curly');
@moreNames = ('Moe', 'Shemp');
push (@myNames, @moreNames);

Note for beginning programmers: Perl arrays begin with an @ symbol. Each complete line of code must end with a semicolon. If it doesn't, it won't execute. In the stacked example in this article, the lines without a semicolon are values contained in an array and enclosed in parentheses. This isn't an exception to the semicolon rule, as much as a result of the stack approach. The values in the array are not individual lines of code. It is easier to picture this in the horizontal approach to coding.

给初学者的注意事项:   Perl数组以@符号开头。 每行完整的代码行必须以分号结尾。 如果没有,它将不会执行。 在本文的堆叠示例中,不带分号的行是包含在数组中并括在括号中的值。 作为堆栈方法的结果,这也不是分号规则的例外。 数组中的值不是单独的代码行。 在水平编码方法中更容易描述这一点。

其他操作数组的功能 ( Other Functions for Manipulating Arrays )

Other functions are also used to manipulate arrays. These make it easy and efficient to use a Perl array as a stack or as a queue. In addition to the push function, you can use:

其他功能也用于操纵数组。 这些使将Perl数组用作堆栈或队列变得容易而高效。 除了推送功能之外,您还可以使用:

  • Pop function – removes and returns the last element of an array

    弹出函数 –删除并返回数组的最后一个元素

  • Shift function – moves the whole array to the left. The element that is the first element of the array falls off the array and becomes the return value of the function

    移位功能 –将整个数组向左移动。 作为数组第一个元素的元素脱离数组,成为函数的返回值

  • Unshift function – the opposite of the shift function, places a value at the beginning of an array and moves all the other element to the right.

    取消移位功能 –与移位功能相反,将值放置在数组的开头,并将所有其他元素向右移动。

翻译自: https://www.thoughtco.com/perl-array-push-function-quick-tutorial-2641151

perl push函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值