[請益] 迴圈內變數複製
各位前輩大家好。
目前手上在寫一個整理資料的工具時,碰到了底下問題,希望有空的前輩可以解答。
程式碼如下。
問題在於為什麼之後在調用陣列@sort並打印的時候,會出現未初始化的訊息呢?
Use of uninitialized value $sort[0] in concatenation (.) or string at TRY.pl
line 32.
*****
$file = "XXX.txt";
$out= "Sorting_$file";
our @sort;
our @array;
open(FHD,"<$file") || die "Can't open $file!\n";
open(FHO,">$out") || die "Can't open $file!\n";
#Data copy to array#
$n=0;
while ($line=<FHD>) {
$array[$n] = $line;
$n++;
}
close(FHD);
#Data sorting
$F=0;
for($i=0; $i<=$#array; $i++)
{
if ($array[$i]=~ /^</ || $array[$i]=~ /\(F\)/ || $array[$i]=~ /\(A\)/ ||
$array[$i]=~ /{-1}/)
{
$sort[$F] = $array[$i];
# print FHO "$sort[$F]";
}
$F++;
}
print "Array lenth= $#array\n";
print FHO "$array[0]\n";
close(FHO);
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.251.162.25
※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1515678670.A.9BF.html
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章