📜  珀尔 | unshift()函数

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

珀尔 | unshift()函数

Perl 中的 unshift()函数将给定的元素列表放在数组的开头。从而将数组中的所有值向右移动。使用此操作可以取消移位多个值。此函数返回数组中新元素的数量。

示例 1:

Perl
#!/usr/bin/perl
   
# Initializing the array
@x = ('Geeks', 'for', 'Geeks');
   
# 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, 'Welcome', 'to');
   
# Array after unshift operation
print "\nUpdated array: @x";


Perl
#!/usr/bin/perl
   
# Initializing the array
@x = (10, 20, 30, 40, 50);
   
# 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, 70, 80, 'Geeks');
   
# Array after unshift operation
print "\nUpdated array: @x";


输出:
Original array: Geeks for Geeks 
No of elements returned by unshift: 5
Updated array: Welcome to Geeks for Geeks

示例 2:

Perl

#!/usr/bin/perl
   
# Initializing the array
@x = (10, 20, 30, 40, 50);
   
# 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, 70, 80, 'Geeks');
   
# Array after unshift operation
print "\nUpdated array: @x";
输出:
Original array: 10 20 30 40 50 
No of elements returned by unshift: 8
Updated array: 70 80 Geeks 10 20 30 40 50