473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nested while loop won't repeat

2 New Member
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 4460
nithinpes
410 Recognized Expert Contributor
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 Recognized Expert Specialist
or he can use seek() to return to the beginning of the file.
Jul 14 '08 #3
numlock00
2 New Member
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

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

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
9
by: Itay | last post by:
Hi all , Suppose i have structurs defines as follows : struct S1 { int n1 ; int n2 ; int n3 ; };
5
by: horndude77 | last post by:
Ok, this might be for a web designing group, but here's my problem. I'm trying to make a web page with tabs which you can navigate between without the page reloading. I have one set of tabs working...
0
by: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: mark | last post by:
(not sure if this is the correct group) My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
4
by: henry | last post by:
Folks: Using Dreamweaver CS3... Consider a home page, "index.php" which conditionally REQUIREs one of 'N' HTML files of pure content. All site styles are specified in a master CSS file,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.