469,331 Members | 5,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

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:
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
3 4229
nithinpes
410 Expert 256MB
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
4,059 Expert 2GB
or he can use seek() to return to the beginning of the file.
Jul 14 '08 #3
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.

Similar topics

25 posts views Thread by chad | last post: by
9 posts views Thread by Itay | last post: by
5 posts views Thread by horndude77 | last post: by
reply views Thread by mark | last post: by
5 posts views Thread by Martin Schou | last post: by
9 posts views Thread by Javaman59 | last post: by
2 posts views Thread by mark | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.