Perl 数组 pop() 函数
Perl中的pop()函数返回作为参数传递给它的数组的最后一个元素,将该值从数组中删除。注意,传递给它的值必须明确是一个数组,而不是一个列表。
语法:
pop(Array)
返回:
undef 如果列表为空,则返回数组中的最后一个元素。
例子 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 ispopped_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 ispopped_element";
# Printing the resultant array
print "\nResultant array after pop(): \n@array";
输出。
Popped element is Best
Resultant array after pop():
Geeks For Geeks