开发者

Why does this for loop inside while exit before the while condition?

开发者 https://www.devze.com 2023-02-19 08:56 出处:网络
I am reading a file and executing a while loop to iteratively run through each line in the file. Inside the while loop I have a if condition which has a nested for loop which does some processes. What

I am reading a file and executing a while loop to iteratively run through each line in the file. Inside the while loop I have a if condition which has a nested for loop which does some processes. What happens is that the code exits once the for loop finishes despite the fact that the condition to keep the while loop running is still valid.

my $datafile = "data.txt";
open my $fh, "<", $datafile or die "Failed to open $datafile ($!)";

my $csv = Text::CSV->new() or die "Failed to create Text::CSV object";
my $line = <$fh>;

while ($line = <$fh>)
{ 
    blah blah
    blah blah

    if ($foo > $cow) {
        blah
    } elsif ($foo == $cow) { 
        blah
    } 开发者_如何转开发else {
        for ($i =1; $i <= $something; $i++) {
            blah blah
        }
    }
}

The problem is the code is not finished going through every line of the data.txt file. The code exits once it finishes with the for loop.

Any insight would be greatly appreciated. Thank you.


I suggest you use the debugger to see what is happening. perldoc perldebug

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号