編程的新手。任務是從一個字符串中提取特定的數據,我選擇瞭如下編寫代碼 -更好的變量命名系統?
while ($line =<IN>) {
chomp $line;
@tmp=(split /\t/, $line);
next if ($tmp[0] !~ /ch/);
@[email protected][8..11];
@[email protected][12..14];
@[email protected][15..17];
@[email protected][18..21];
foreach (1..4) {
print @tgt($_), "\n";
}
我想@tgt($_)
將被解釋爲@tgt1, @tgt2, @tgt3, @tgt4
,但我仍然得到錯誤信息@tgt
是一個全球性的符號( @ tgt1,@ tgt2,@ tgt3,@ tgt4`已被聲明)。
Q1。我誤解了foreach循環嗎? Q2302。爲什麼不能perl看到@tgt($_)
作爲@ tgt1,@ tgt2 ..等等? Q2302。從經驗來看,這可能是命名變量的不好方法。命名具有相似特徵的變量的首選方式是什麼?
始終使用'使用嚴格;使用警告;' – ikegami
對不起,Ikegami,我確實使用嚴格和警告,但我只粘貼了部分我認爲相關的代碼(正如警告和錯誤所示)。 –
@BChen - 令我驚訝的是,@ tgt($ _)'甚至編譯:) – DVK