Link to home
Start Free TrialLog in
Avatar of Paulmul
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
Avatar of dorward
dorward

Put the code that generates the content type header outside (before) the loop.
Avatar of Paulmul

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();  
     PrintHead();
##Part that is looping
       print "<br>$i of $Time1 iterations</br>";
## End of part that is looping
     PrintTail();  
Avatar of Paulmul

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?
Avatar of ozo
Why do you say you can't do this?
Avatar of Paulmul

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?
Avatar of Paulmul

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?
Avatar of Paulmul

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.
Why can't you just print the header before you enter the loop?
Avatar of Paulmul

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?
Avatar of Paulmul

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?
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paulmul

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
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;
Avatar of Paulmul

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.
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.
Avatar of Paulmul

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paulmul

ASKER

healthstats, I'll try that next week. Thanks.
Avatar of Paulmul

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.