Perl中的unshift()函數將給定的元素列表放置在數組的開頭。從而將數組中的所有值右移。使用此操作可以取消多個值的移位。此函數返回數組中新元素的數量。
用法: unshift(Array, List)
返回值:數組中新元素的數量
示例1:
#!/usr/bin/perl
# Initalizing the array
@x = ('Geeks', 'for', 'Geeks');
# Print the Inital 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";
輸出:
Original array: Geeks for Geeks No of elements returned by unshift: 5 Updated array: Welcome to Geeks for Geeks
示例2:
#!/usr/bin/perl
# Initalizing the array
@x = (10, 20, 30, 40, 50);
# Print the Inital 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
相關用法
- Perl each()用法及代碼示例
- Perl int()用法及代碼示例
- Perl sin()用法及代碼示例
- Perl tell()用法及代碼示例
- Perl chr()用法及代碼示例
- Perl exp用法及代碼示例
- Perl hex用法及代碼示例
- Perl cos()用法及代碼示例
- Perl abs()用法及代碼示例
- Perl log()用法及代碼示例
- Perl uc()用法及代碼示例
- Perl ord()用法及代碼示例
- Perl oct()用法及代碼示例
- Perl quotemeta()用法及代碼示例
注:本文由純淨天空篩選整理自Code_Mech大神的英文原創作品 Perl | unshift() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。