FOR和WHILE 的一点很重要的区别
时间:2008-05-16 来源:yjc0407
=head1 DESCRIPTION
the different of while and for;
only next exist the loop increase in for will increase
when exist for(),for example for ($i;$i<10;$i++) but if so for($i;$i<10;){$i++} the $i will not increase
if appear next,the same as next in while loop;
use strict;
use warnings;
sub test_for{
my $filename = shift @_;
local (*FN);
my(@dt,@dc,@de);
my ($i,$BNR);
@dt = @dc = @de = ();
open FN,"<$filename";
for ($i=0; <FN>;++$i){
chomp;
($dt[$i],$dc[$i],$de[$i]) = split /\s+/;
next if ("002" lt substr($dt[$i],3,3));
last if ("M" lt substr($de[$i],0,1));
print substr($de[$i],0,1);
#++$i;
}
close(FN);
$BNR = $i;
print $BNR;
}
my $fil = shift(@ARGV);
test_for($fil);
=POD file.txt
$ARGV[0] = file.txt
20000104 -0.85 L
20000104 -0.85 Y
20000412 4.65 Y
20000412 4.65 IBM
20000314 -9.18 MSFT
20000331 -6.80 MSFT
= cut
the different of while and for;
only next exist the loop increase in for will increase
when exist for(),for example for ($i;$i<10;$i++) but if so for($i;$i<10;){$i++} the $i will not increase
if appear next,the same as next in while loop;
use strict;
use warnings;
sub test_for{
my $filename = shift @_;
local (*FN);
my(@dt,@dc,@de);
my ($i,$BNR);
@dt = @dc = @de = ();
open FN,"<$filename";
for ($i=0; <FN>;++$i){
chomp;
($dt[$i],$dc[$i],$de[$i]) = split /\s+/;
next if ("002" lt substr($dt[$i],3,3));
last if ("M" lt substr($de[$i],0,1));
print substr($de[$i],0,1);
#++$i;
}
close(FN);
$BNR = $i;
print $BNR;
}
my $fil = shift(@ARGV);
test_for($fil);
=POD file.txt
$ARGV[0] = file.txt
20000104 -0.85 L
20000104 -0.85 Y
20000412 4.65 Y
20000412 4.65 IBM
20000314 -9.18 MSFT
20000331 -6.80 MSFT
= cut
相关阅读 更多 +