Perl shift()用法及代碼示例- 純淨天空
文章推薦指數: 80 %
Perl中的shift()函數返回數組中的第一個值,將其刪除並將數組列表中的元素向左移動一個。
Shift操作會刪除pop等值,但它是從數組的開頭而不是pop的結尾獲取的。
當前位置:首頁>>代碼示例
>>用法及示例精選
>>正文
Perl中的shift()函數返回數組中的第一個值,將其刪除並將數組列表中的元素向左移動一個。
Shift操作會刪除pop等值,但它是從數組的開頭而不是pop的結尾獲取的。
如果數組為空,則此函數返回undef,否則返回數組的第一個元素。
用法:shift(Array)
返回值:如果數組為空,則為-1,否則為數組的第一個元素
示例1:
#!/usr/bin/perl-w
#DefiningArraytobeshifted
@array1=("Geeks","For","Geeks");
#OriginalArray
print"OriginalArray:@array1\n";
#Performingtheshiftoperation
$shifted_element=shift(@array1);
#Printingtheshiftedelement
print"Shiftedelement:$shifted_element\n";
#UpdatedArray
print"UpdatedArray:@array1";
輸出:
OriginalArray:GeeksForGeeks
Shiftedelement:Geeks
UpdatedArray:ForGeeks
示例2:
#!/usr/bin/perl-w
#Programtomovefirstelement
#ofanarraytotheend
#DefiningArraytobeshifted
@array1=("Geeks","For","Geeks");
#OriginalArray
print"OriginalArray:@array1\n";
#Performingtheshiftoperation
$shifted_element=shift(@array1);
#PlacingFirstelementintheend
@array1[3]=$shifted_element;
#UpdatedArray
print"UpdatedArray:@array1";
輸出:
OriginalArray:GeeksForGeeks
UpdatedArray:ForGeeksGeeks
相關用法
Perlexp用法及代碼示例
Perlhex用法及代碼示例
Perlabs()用法及代碼示例
Perlchr()用法及代碼示例
Perllog()用法及代碼示例
Perloct()用法及代碼示例
Perlord()用法及代碼示例
Perleach()用法及代碼示例
Perlcos()用法及代碼示例
Perlsin()用法及代碼示例
Perlint()用法及代碼示例
Perltell()用法及代碼示例
Perluc()用法及代碼示例
PerlAUTOLOAD用法及代碼示例
注:本文由純淨天空篩選整理自Code_Mech大神的英文原創作品 Perl|shift()Function。
非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
延伸文章資訊
- 1Manipulating Perl arrays: shift, unshift, push, pop - Perl Maven
If you imagine the array starting on the left hand side, the shift function will move the whole a...
- 2perl程序中shift的用法 - 百度知道
看到有些perl程序写子程序submysub{my$pk=shift;}这里新定义的变量$pk,为什么要直接用shift,这里的shift起到什么作用?我只知道shift是从数组开头移除 ...
- 3shift in Perl - Perl Maven
The shift function in Perl will remove the first value of the array passed to it and return it. ....
- 4Perl shift()用法及代碼示例- 純淨天空
Perl中的shift()函數返回數組中的第一個值,將其刪除並將數組列表中的元素向左移動一個。 Shift操作會刪除pop等值,但它是從數組的開頭而不是pop的結尾獲取的。
- 5Perl中shift函数用法 - CSDN博客
数组也被改变,其第一个元素被弹出。 如果没有array,直接给变量赋值shift. @ARGV: perl 命令行参数. 当运行perl脚本的时候,从 ...