By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 2,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

Nested while loop won't repeat

P: 2
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.  
Jul 14 '08 #1
Share this Question
Share on Google+
3 Replies


nithinpes
Expert 100+
P: 410
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.  
Jul 14 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
or he can use seek() to return to the beginning of the file.
Jul 14 '08 #3

P: 2
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

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.  
Jul 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.