珀尔 |数组 pop()函数
Perl 中的 pop()函数返回作为参数传递给它的 Array 的最后一个元素,从数组中删除该值。请注意,传递给它的值必须明确地是一个数组,而不是一个列表。
Syntax:
pop(Array)
Returns:
undef if list is empty else last element from the array.
示例 1:
#!/usr/bin/perl -w
# Defining an array of integers
@array = (10, 20, 30, 40);
# Calling the pop function
$popped_element = pop(@array);
# Printing the Popped element
print "Popped element is $popped_element";
# Printing the resultant array
print "\nResultant array after pop(): \n@array";
输出:
Popped element is 40
Resultant array after pop():
10 20 30
示例 2:
#!/usr/bin/perl -w
# Defining an array of strings
@array = ("Geeks", "For", "Geeks", "Best");
# Calling the pop function
$popped_element = pop(@array);
# Printing the Popped element
print "Popped element is $popped_element";
# Printing the resultant array
print "\nResultant array after pop(): \n@array";
输出:
Popped element is Best
Resultant array after pop():
Geeks For Geeks