我有以下代碼
@ar1 = ('kaje','beje','ceje','danjo');
$m = 'kajes';
my($next) = grep $_ eq 'kaje',@ar1;
print("Position is $next\n");
print("Next is $ar1[$next 1]\n");
print("Array of c is $ar1[$m 3]\n");
print("Array at m is $ar1[$m]\n");
看到的輸出:
Position is kaje
Next is beje
Array of c is danjo
Array at m is kaje
我想了解這是如何作業的。這里 $next 是匹配的字串,我可以為給定陣列在該字串上建立索引。在陣列中也沒有找到 $m ,但我們將輸出作為陣列的第一個元素。
uj5u.com熱心網友回復:
總是使用use strict; use warnings;. 它回答了你的問題。
在一個地方,您將字串kaje視為一個數字。
在兩個地方,您將字串kajes視為數字。
由于這兩個字串不是數字,Perl 將使用零并發出警告。
換句話說,Next is beje只有“有效”,因為kaje恰好在 index 0。我不知道為什么您認為最后兩行在kajes陣列中無處可見。
你要
# Find the index of the element with value `kaje`.
my ($i) = grep $ar1[$_] eq 'kaje', 0..$#ar1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414585.html
標籤:
