Paulmul
asked on
Loops in Perl
I have a program that pings servers every couple of seconds. For this to work correctly, I have to keep writing to the screen. To do this I am using a loop. The problem is after the first iteration, "Content-Type: text/html" get printed every time it loops. How can I stop this?
Thanks
Thanks
Put the code that generates the content type header outside (before) the loop.
ASKER
The content type header is part of a function called PrintHead() this is called every time it loops as it contains all the html data.
All the loop does is print:
X out of Y iterations
X+1 out of Y itereations, etc.
Is there a way to print this and avoid reprinting, I attached the code below.
##Prints HTML
sub PrintHead {
print << "EndHead";
Content-Type: text/html
<HTML>
<head>
<TITLE>IP details</TITLE>
</head>
<BODY>
<FONT FACE="Arial" SIZE="4" COLOR ="#0000FF">
<!------- HTML HEADER ------------- -->
EndHead
}
##Part that is looping
PrintHead();
print "<br>$i of $Time1 iterations</br>";
PrintTail();
All the loop does is print:
X out of Y iterations
X+1 out of Y itereations, etc.
Is there a way to print this and avoid reprinting, I attached the code below.
##Prints HTML
sub PrintHead {
print << "EndHead";
Content-Type: text/html
<HTML>
<head>
<TITLE>IP details</TITLE>
</head>
<BODY>
<FONT FACE="Arial" SIZE="4" COLOR ="#0000FF">
<!------- HTML HEADER ------------- -->
EndHead
}
##Part that is looping
PrintHead();
print "<br>$i of $Time1 iterations</br>";
PrintTail();
PrintHead();
##Part that is looping
print "<br>$i of $Time1 iterations</br>";
## End of part that is looping
PrintTail();
##Part that is looping
print "<br>$i of $Time1 iterations</br>";
## End of part that is looping
PrintTail();
ASKER
What I need to do is get it to avoid calling printhead() I can't do this though. Is there a way I could have the screen refresh so instead of "$i of $Time1 iterations" being printed several times, the screen refreshes and it only writes to one line?
Why do you say you can't do this?
ASKER
It is the way my code is set up, I can't avoid calling printhead()
What is preventing you from changing the way your code is set up?
Could you show us the code in question?
Could you change printhead() to only print the first time it is called?
Could you show us the code in question?
Could you change printhead() to only print the first time it is called?
ASKER
The code is above, printhead() is called in the loop, I am running things for over an hour at a time. I have to keep in printing to the screen to stop it from timing out. Everything happens in a loop including writing to the screen, this is why it keeps calling printhead(). I need to refresh the screen or something but I do not know how.
Can't you just take the Content-Type: text/html out of the loop?
ASKER
No, it's part of a header that has to be called to print HTML if I remove the line, HTML doesn't print. I don't think this can be changed. I am going to ask for the question to be deleted.
Thanks anyway.
Thanks anyway.
Why can't you just print the header before you enter the loop?
ASKER
It is the whole setup of my code. It does a lot of things at once, if I do that it will mess up.
Without the willingness to change the way your code is set up, how do you expect to get the results that you want?
ASKER
I would have to completely re-write it and this isn't an option. It is part of a project and I can't change it against what I handed in, it was only a small change that I wanted to make for a demonstration, I had hoped I may only need to change one line or something but to re-write that much it is against the rules.
Could you change it to
if ($i == 0) { PrintHead() }
would that work?
if ($i == 0) { PrintHead() }
would that work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried if ($i == 0) { PrintHead() } and it didn't work, it said it couldn't print to the scree (due to missing the printhead()) I was hoping someone could tell me a way around this.
Ozo, I wont be in college for another week (Easter break) I will look at doing your idea then. That could be exactly what I need. Do you know if that works or are you just guessing? As I said above I couldn't use if, will this be any different?
Thanks
Ozo, I wont be in college for another week (Easter break) I will look at doing your idea then. That could be exactly what I need. Do you know if that works or are you just guessing? As I said above I couldn't use if, will this be any different?
Thanks
Jumping in here, if ($i == 0) { PrintHead() } won't work, because you never set $i to 0, you create $i with no value, then you assign $i=1; so you can either:
our $i=0;
or
if(!$i){ PrintHead() } but it would be more elegant to
PrintHead() unless $i;
our $i=0;
or
if(!$i){ PrintHead() } but it would be more elegant to
PrintHead() unless $i;
ASKER
I tried that way, including initialising i to 0. The error it gave me was that it couldn't find the print headers section (printhead()) do you actually know if the 'unless' method will get around this error?
You are being extremely vague in what and where you are changing things. printhead() doesn't equal PrintHead() you shouldn't be calling PrintHead from within itself. It sounds like if you will put
use strict;
up at the top of your code a lot of your problems may go away.
use strict;
up at the top of your code a lot of your problems may go away.
The use of the strict pragma is certianly highly recommended, but I'd say that adding it in at this point would almost certianly "break" other sections of the code.
I agree that the info that we've been given so far is too vauge for us to be able to provide the best answers. If ozo's last suggestion doesn't work then from the info that we've been given, I'd say it can't be done without modifing other sections of the code.
I agree that the info that we've been given so far is too vauge for us to be able to provide the best answers. If ozo's last suggestion doesn't work then from the info that we've been given, I'd say it can't be done without modifing other sections of the code.
ASKER
Printhead() is a sub routine, it has all the html headers in it. The program runs for an hour at more, to stop apache from timing out, it has to print something every iteration. To do this I have to put something to print in the loop (The loop is what runs the prgram). To print I have to call the PrintHead() function as this is what makes it html. every time I call PrintHead() it also prints the "context-tpye..." I need to find away around this without changing my subroutine structure (PrintHead()) this is my dilemma.
I use strict in all my programs.
I use strict in all my programs.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
healthstats, I'll try that next week. Thanks.
ASKER
I can't remember what I did in the end so I am going to split the points between healthStatus and Ozo. Sorry I didn't do this earlier, I completely forgot about the question.