珀尔 |数组(push、pop、shift、unshift)
Perl 提供了各种内置函数来添加和删除数组中的元素。
Function Description push Inserts values of the list at the end of an array pop Removes the last value of an array shift Shifts all the values of an array on its left unshift Adds the list element to the front of an array
此函数将列表中给定的值插入到数组的末尾。可以插入多个值,以逗号分隔。这个函数增加了数组的大小。它返回新数组中的元素数。
Syntax: push(Array, list)
例子:
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
# Printing the array
print "Updated array: @x";
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the value returned by pop
print "Value returned by pop: ", pop(@x);
# Prints the array after pop operation
print "\nUpdated array: @x";
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the value returned
# by shift function
print "Value returned by shift: ",
shift(@x);
# Array after shift operation
print "\nUpdated array: @x";
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the number of elements
# returned by unshift
print "No of elements returned by unshift: ",
unshift(@x, 'PHP', 'JSP');
# Array after unshift operation
print "\nUpdated array: @x";
输出:
Original array: Java C C++
Updated array: Java C C++ Python Perl
此函数用于删除数组的最后一个元素。执行 pop函数后,数组的大小减一。如果列表为空,此函数返回 undef,否则返回数组的最后一个元素。
Syntax: pop(Array)
例子:
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \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++
Value returned by pop: C++
Updated array: Java C
此函数返回数组中的第一个值,将其删除并将数组列表的元素向左移动一个。移位操作像 pop 一样删除值,但从数组的开头而不是 pop 的结尾取值。如果数组为空,此函数返回 undef,否则返回数组的第一个元素。
Syntax: shift(Array)
例子:
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the value returned
# by shift function
print "Value returned by shift: ",
shift(@x);
# Array after shift operation
print "\nUpdated array: @x";
输出:
Original array: Java C C++
Value returned by shift :Java
Updated array: C C++
此函数将给定的元素列表放在数组的开头。从而将数组中的所有值向右移动。使用此操作可以取消移位多个值。此函数返回数组中新元素的数量。
Syntax: unshift(Array, List)
例子:
Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the number of elements
# returned by unshift
print "No of elements returned by unshift: ",
unshift(@x, 'PHP', 'JSP');
# Array after unshift operation
print "\nUpdated array: @x";
输出:
Original array: Java C C++
No of elements returned by unshift :5
Updated array: PHP JSP Java C C++