全部,
考虑这段代码:
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>;
}
最后一个打印语句不想打印变量。即使我将其声明为“我们的”或只是将声明注释掉。更奇怪的是它只发生在第一次迭代中。该文件有几千条记录,在第一次迭代中只有变量未定义。所有后续调用都很好。
知道发生了什么吗?
谢谢你。