perl splice
The Perl splice function takes the following form:
Perl拼接功能采用以下形式:
Perl's splice() function is used to cut out and return a chunk or portion of an array. The portion that is cut out starts at the OFFSET element of the array and continues for LENGTH elements. If the LENGTH is not specified, it will cut to the end of the array.
Perl的splice()函数用于剪切并返回数组的一部分或一部分。 切出的部分从数组的OFFSET元素开始,然后继续到LENGTH元素。 如果未指定LENGTH ,它将切到数组的末尾。
Perl拼接功能的示例 ( Example of the Perl Splice Function )
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered starting with a zero. The splice() function would cut a chunk out of the @myNames array starting with the element in the #1 position (in this case, Michael) and ending 3 elements later at Matthew. The value of @someNames then becomes ('Michael', 'Joshua', 'Matthew'), and @myNames is shortened to ('Jacob', 'Ethan', 'Andrew').
将@myNames数组视为一行带编号的框,从左到右,从零开始编号。 splice()函数将从@myNames数组中切出一个块(从#1位置的元素开始(在本例中为Michael ),然后在Matthew结束3个元素)。 然后@someNames的值变为('Michael','Joshua','Matthew') ,而@myNames缩短为('Jacob','Ethan','Andrew') 。
使用可选的“ REPLACE_WITH” ( Using the Optional 'REPLACE_WITH' )
As an option, you can replace the portion removed with another array by passing it in the REPLACE_WITH argument.
作为一种选择,您可以通过在REPLACE_WITH参数中传递被删除的部分来替换为另一个数组。
In the above example, the splice() function would cut a chunk out of the @myNames array starting with the element in the #1 position (in this case, Michael and ending 3 elements later at Matthew. It then replaces those names with the contents of the @moreNames array. The value of @someNames then becomes ('Michael', 'Joshua', 'Matthew'), and @myNames is changed to ('Jacob', 'Daniel', 'William', 'Joseph', 'Ethan', 'Andrew').
在上面的示例中, splice()函数将从@myNames数组中切出一个块(从#1位置的元素开始(在这种情况下, Michael到3个元素在Matthew的结尾),然后将这些名称替换为所述@moreNames数组的内容。@someNames的值然后变为(“迈克尔”,“约书亚”,“马修”),和@myNames改变为(“雅各”,“丹尼尔”,“威廉”,'约瑟夫,“伊桑”,“安德鲁”) 。
You might want to check out some other Perl array functions such as reverse() to reverse the order of your array.
您可能需要检查其他一些Perl数组函数,例如reverse()来反转数组的顺序 。
翻译自: https://www.thoughtco.com/perl-array-splice-function-quick-tutorial-2641163
perl splice