On Thu, 2008-08-07 at 18:42 +0800, Jeff Pang wrote: > Anees 写道: > > > > #!/usr/bin/perl > > $maxRecords=20000; > > $steps=100; > > > > $index=1; > > $recCounter=0; > > while (<> && defined){ > > > This is not right. > defined is equal to defined($_), but here $_ go without value. > For reading a file line by line, saying: > > while(<>) { ... } > > is enough. > > while(<>) { > if (defined) {...} > } > > this defined is useless. b/c if $_ is undefined in while(), the loop > will be broken already. > > I guess you want to exclude the blank lines, then just say: > > next if /^$/; > > > > > if($recCounter > $maxRecords){ break; } > > break is a wrong statement. > You should replace "break" with "last" here, perl does it not the same as C. > > > > > $index++; > > } > > You could never use a explict variable for keeping the line number, perl > has the built-in variable "$." for that purpose. see "perldoc perlvar". > > HTH. >
I must point out that if you have the statement: next if /^$/; earlier in the loop, $index is counting the non-blank lines and is not the same as "$." -- Just my 0.00000002 million dollars worth, Shawn "Where there's duct tape, there's hope." "Perl is the duct tape of the Internet." Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/