📜  珀尔 | shift()函数

📅  最后修改于: 2022-05-13 01:54:51.895000             🧑  作者: Mango

珀尔 | shift()函数

Perl 中的 shift()函数返回数组中的第一个值,删除它并将数组列表的元素向左移动一个。移位操作像 pop 一样删除值,但从数组的开头而不是 pop 的结尾取值。如果数组为空,此函数返回 undef,否则返回数组的第一个元素。

示例 1:

#!/usr/bin/perl -w
  
# Defining Array to be shifted
@array1 = ("Geeks", "For", "Geeks");
  
# Original Array
print "Original Array: @array1\n";
  
# Performing the shift operation
$shifted_element = shift(@array1);
  
# Printing the shifted element
print "Shifted element: $shifted_element\n";
  
# Updated Array
print "Updated Array: @array1";
输出:
Original Array: Geeks For Geeks
Shifted element: Geeks
Updated Array: For Geeks

示例 2:

#!/usr/bin/perl -w
  
# Program to move first element 
# of an array to the end
  
# Defining Array to be shifted
@array1 = ("Geeks", "For", "Geeks");
  
# Original Array
print "Original Array: @array1\n";
  
# Performing the shift operation
$shifted_element = shift(@array1);
  
# Placing First element in the end
@array1[3] = $shifted_element;
  
# Updated Array
print "Updated Array: @array1";
输出:
Original Array: Geeks For Geeks
Updated Array: For Geeks  Geeks