crisa89 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm parsing through a file and using multiple "if" and "while(<filename>)" statements to match against the file. When I do one "if" statement, it skips over the second, and so on. Second "if" only works when I restate the "while" and so on. Is there a better way to parse through a file and still match? Is there a way I can use multiple "if"? Below is the code (by the way, some lines are commented out because I was debugging. So they were originally not commented out):

if(/scanchain/){ $flag0 = 1; #while($flag0){ print "in scanchain while loop\n"; $flag1 = 1; #while($flag1){ if (/(\s+)chain(\d+)_chain \[/){ print "Why hello there?\n"; $scanchain_chain_no = $2; #$flag = 1; #while($flag){ #for($t=0; $t<=$Chain_No_size; $t++){ #print "in for loop\n"; #while(<WGL>){ #print "Coo\n"; #if($Chain_No[$t] eq $scanchain_chain_ +no){ #print "too\n"; #print "$Chain_No[$t]\n"; #my $Input_Pin = <WGL>; + #$Input_Pin =~ tr/"//d; + #$Input_Pin =~ tr/,//d; #$Input_Pin =~ tr/ //d; + #print "Chain $Chain_No[$t] Input Pin += $Input_Pin\n"; #while(<WGL>){ #if(/(\s+)"(.*)"/){ #$quotes = $2; # foreach ($quotes){ # push @Quotes, $quotes;} #} #else{ last; } #} #$Output_Pin=$Quotes[-1]; #print "Chain $Chain_No[$t] Output Pin + = $Output_Pin\n\n"; #} #next; #} #} #next; #} } #} #} }

Replies are listed 'Best First'.
Re: Nested if and while statements
by AppleFritter (Vicar) on Jun 23, 2014 at 22:36 UTC

    I'm not sure what you mean by "[w]hen I do one "if" statement, it skips over the second, and so on. Second "if" only works when I restate the "while" and so on". Could you clarify?

    I'm guessing that your problem's to do with the fact you're exhausting your filehandle. while(<WGL>) will read the entire file, line by line; if you then attempt to read from it again, e.g. in a second while loop further down in a similar block of code, it'll try to continue where you last left off, notice it's already at the end of the file, and not execute the loop body even once.

    That's easily remedied by using seek, though: seek WGL, 0, SEEK_SET will move the filehandle's position to the beginning of the file again.

    Is that the problem you're having?

Re: Nested if and while statements
by hippo (Archbishop) on Jun 23, 2014 at 22:34 UTC

    Your regex tests are happening against $_ which is context-sensitive, ie. it will be changed by the loops. If that isn't what you want, then test against a persistent lexical variable instead. See perlvar for all the details on $_ and friends.

Re: Nested if and while statements
by Laurent_R (Canon) on Jun 24, 2014 at 06:36 UTC
    I agree with AppleFritter and would add further that all these nested while and for loops make very little sense if your whole file is going to be read in just one swoop of the most inner while (<WGL>) loop. So your outer loops are executed usefully only once, which means you might not need them as loops. Or, possibly, you need some condition to go out of the most inner loop.