Nested while loop won't repeat

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • numlock00
    New Member
    • Jul 2008
    • 2

    Nested while loop won't repeat

    I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to apply each of these elements while reading an input file.

    The outer loop is working fine. It will run through every element of the array. The inner loop, however, only runs once. Even though the outer loop finishes inormally, the inner loop does not repeat.

    Here it is:
    Code:
    foreach $hour (@hourRange) {
    	$timeBlock = $timeDateBase . $hour;
    	print "$timeBlock\n";
    
    	while (<LOG>) {
    		chomp;
    		if ($_ =~ /$timeBlock/) {
    			print "$timeBlock\n";
    			($a,$b,$c) = split(/"/, $_);
    			if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
    				$IP = $1;
    				$timeString = $2;
    				print "$IP\n";
    				print "$timeString\n";
    			}	
    		}
    	}
    }
    Last edited by numberwhun; Jul 14 '08, 01:35 PM. Reason: p
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    You are using LOG filehandle in the while() loop. Where are you opening the file?
    However, in the first iteration of foreach() loop itself, entire content of file would be read(EOF would be reached) and it would not be available to be read in the second iteration.
    To avoid this, open() function for opening file should be declared within foreach loop and the file handle should be closed before going for next iteration of foreach loop. So that, file will be opened and read with each iteration of foreach() loop.

    Code:
    foreach $hour (@hourRange) {
    $timeBlock = $timeDateBase . $hour;
    print "$timeBlock\n";
    
    open(LOG,"logfile.txt") or die "cannot open:$!";
    
    while (<LOG>) {
    chomp;
    if ($_ =~ /$timeBlock/) {
    print "$timeBlock\n";
    ($a,$b,$c) = split(/"/, $_);
    if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
    $IP = $1;
    $timeString = $2;
    print "$IP\n";
    print "$timeString\n";
    } 
    }
    }
    close(LOG);
    }

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      or he can use seek() to return to the beginning of the file.

      Comment

      • numlock00
        New Member
        • Jul 2008
        • 2

        #4
        Thank you. Your recommendation worked. I eventually used seek() instead because it seemed more economical, but the logic is the same in both cases. Your help is much appreciated.

        numlock00

        Originally posted by nithinpes
        You are using LOG filehandle in the while() loop. Where are you opening the file?
        However, in the first iteration of foreach() loop itself, entire content of file would be read(EOF would be reached) and it would not be available to be read in the second iteration.
        To avoid this, open() function for opening file should be declared within foreach loop and the file handle should be closed before going for next iteration of foreach loop. So that, file will be opened and read with each iteration of foreach() loop.

        Code:
        foreach $hour (@hourRange) {
        $timeBlock = $timeDateBase . $hour;
        print "$timeBlock\n";
        
        open(LOG,"logfile.txt") or die "cannot open:$!";
        
        while (<LOG>) {
        chomp;
        if ($_ =~ /$timeBlock/) {
        print "$timeBlock\n";
        ($a,$b,$c) = split(/"/, $_);
        if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
        $IP = $1;
        $timeString = $2;
        print "$IP\n";
        print "$timeString\n";
        } 
        }
        }
        close(LOG);
        }

        Comment

        Working...