📜  珀尔 |有用的数组函数

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

珀尔 |有用的数组函数

在 Perl 中,数组是一种特殊类型的变量。数组用于存储值列表,列表中的每个对象称为一个元素。元素可以是数字、字符串或任何类型的标量数据,包括另一个变量。
Perl 中的数组提供了各种内置函数来执行诸如从预定义数组中添加和删除元素之类的操作。
例子:

Perl
#!/usr/bin/perl
   
# Initializing the array
@x = ('Java', 'C', 'C++');
   
# Print the Initial array
print "Original array: @x \n";
 
# Using push() function
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
print("Pushing new values...\n");
   
# Printing the array
print "Updated array: @x\n";
 
# Using pop() function
print("\nPopping the last element...\n");
 
# Prints the value returned by pop
print "Value returned by pop: ", pop(@x);
   
# Prints the array after pop operation
print "\nUpdated array: @x";


输出:
Original array: Java C C++ 
Pushing new values...
Updated array: Java C C++ Python Perl

Popping the last element...
Value returned by pop: Perl
Updated array: Java C C++ Python


下面列出了一些有用的数组函数:

FunctionDescription
push()Used to push a list of values onto the end of the array
pop()Returns the last element of Array passed to it as an argument, removing that value from the array
shift()Returns the first value in an array, removing it and shifting the elements of the array list to the left by one
unshift()Places the given list of elements at the beginning of an array, shifting all the values to the right
sort()Used to sort a list with or without the use of method of sorting
wantarray()Returns True if the currently executing subroutine expects to return a list value, and false if it is looking for a scalar value.
exists()Used to check whether an element in an given array or hash exists or not
grep()Used to extract any element from the given array which evaluates the true value for the given regular expression
join()Combines the elements of LIST into a single string using the value of VAR to separate each element