Connecting Tech Pros Worldwide Forums | Help | Site Map

Nested while loop won't repeat

Newbie
 
Join Date: Jul 2008
Posts: 2
#1: Jul 14 '08
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:
Expand|Select|Wrap|Line Numbers
  1. foreach $hour (@hourRange) {
  2.     $timeBlock = $timeDateBase . $hour;
  3.     print "$timeBlock\n";
  4.  
  5.     while (<LOG>) {
  6.         chomp;
  7.         if ($_ =~ /$timeBlock/) {
  8.             print "$timeBlock\n";
  9.             ($a,$b,$c) = split(/"/, $_);
  10.             if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
  11.                 $IP = $1;
  12.                 $timeString = $2;
  13.                 print "$IP\n";
  14.                 print "$timeString\n";
  15.             }    
  16.         }
  17.     }
  18. }
  19.  

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Jul 14 '08

re: Nested while loop won't repeat


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.

Expand|Select|Wrap|Line Numbers
  1. foreach $hour (@hourRange) {
  2. $timeBlock = $timeDateBase . $hour;
  3. print "$timeBlock\n";
  4.  
  5. open(LOG,"logfile.txt") or die "cannot open:$!";
  6.  
  7. while (<LOG>) {
  8. chomp;
  9. if ($_ =~ /$timeBlock/) {
  10. print "$timeBlock\n";
  11. ($a,$b,$c) = split(/"/, $_);
  12. if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
  13. $IP = $1;
  14. $timeString = $2;
  15. print "$IP\n";
  16. print "$timeString\n";
  17. }
  18. }
  19. close(LOG);
  20.  
  21.  
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Jul 14 '08

re: Nested while loop won't repeat


or he can use seek() to return to the beginning of the file.
Newbie
 
Join Date: Jul 2008
Posts: 2
#4: Jul 15 '08

re: Nested while loop won't repeat


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

Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. foreach $hour (@hourRange) {
  2. $timeBlock = $timeDateBase . $hour;
  3. print "$timeBlock\n";
  4.  
  5. open(LOG,"logfile.txt") or die "cannot open:$!";
  6.  
  7. while (<LOG>) {
  8. chomp;
  9. if ($_ =~ /$timeBlock/) {
  10. print "$timeBlock\n";
  11. ($a,$b,$c) = split(/"/, $_);
  12. if ($a =~ /^(\S+)\s-\s-\s\[(\S+)\s/) {
  13. $IP = $1;
  14. $timeString = $2;
  15. print "$IP\n";
  16. print "$timeString\n";
  17. }
  18. }
  19. close(LOG);
  20.  
  21.  

Reply


Similar Perl bytes