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: -
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";
-
}
-
}
-
}
-
}
-
3 4460
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. -
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);
-
}
-
-
KevinADC 4,059
Recognized Expert Specialist
or he can use seek() to return to the beginning of the file.
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. -
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);
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: Itay |
last post by:
Hi all ,
Suppose i have structurs defines as follows :
struct S1
{
int n1 ;
int n2 ;
int n3 ;
};
|
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...
|
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...
|
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;...
| |
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 = ...;
....
}
}
|
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...
|
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....
|
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,...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |