Perl shift()用法及代碼示例- 純淨天空

文章推薦指數: 80 %
投票人數:10人

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。

非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。



請為這篇文章評分?