ALL,如何正確聲明全局變量?
考慮這段代碼:
our $classwork = 0;
while(<INFILE>)
{
chomp;
next if $. == 1;
if($year >= 1992 && $year <= 1995)
{
$classwork = (split(/,/))[6];
print "Classwork is: ", $classwork, "\n";
}
if($year >= 1996 && $year <= 2001)
{
$classwork = (split(/,/))[-1];
}
print "Classwork is: ", $classwork, "\n";
if($year == 2002)
{
$classwork = (split(/,/))[-2];
}
if($year == 2003 || $year == 2004)
{
$classwork = (split(/,/))[23];
}
if($year >= 2005 && $year <= 2009)
{
$classwork = (split(/,/))[22];
}
if($year >= 2010 && $year <= 2012)
{
$classwork = (split(/,/))[20];
}
print "Classwork is: ", $classwork, "\n";
$line = <STDIN>;
}
最後一個print語句不希望打印的變量。即使我將其聲明爲「我們」,或者僅僅對此聲明發表評論。更奇怪的是,它只發生在第一次迭代。該文件有幾千條記錄,在第一次迭代時只有變量未定義。隨後的所有通話都很好。
任何想法是怎麼回事?
謝謝。
什麼是'$ year'這裏?這是你的腳本的全文嗎? – Suic
可能有一行沒有足夠的字段。 – choroba
我真的很困惑爲什麼你需要在這裏聲明全局變量 – Suic