Solved

Loops in Perl

Posted on 2004-04-07
26
349 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:Paulmul
  • 12
  • 5
  • 3
  • +3
26 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 10774174
Put the code that generates the content type header outside (before) the loop.
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10774889
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();  
0
 
LVL 17

Expert Comment

by:dorward
ID: 10774914
     PrintHead();
##Part that is looping
       print "<br>$i of $Time1 iterations</br>";
## End of part that is looping
     PrintTail();  
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10782492
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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 10782514
Why do you say you can't do this?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10782533
It is the way my code is set up, I can't avoid calling printhead()
0
 
LVL 84

Expert Comment

by:ozo
ID: 10782591
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?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10782697
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 10783679
Can't you just take the Content-Type: text/html out of the loop?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10783864
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 10783917
Why can't you just print the header before you enter the loop?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10784225
It is the whole setup of my code. It does a lot of things at once, if I do that it will mess up.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:FishMonger
ID: 10784393
Without the willingness to change the way your code is set up, how do you expect to get the results that you want?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10785866
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.
0
 
LVL 1

Expert Comment

by:bkiahg
ID: 10786480
Could you change it to

if ($i == 0)  { PrintHead() }

would that work?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 10787635
#would something like
sub PrintHead {
our $i;
print "Content-Type: text/html\r\n\r\n" unless $i;
$i=1;
print << "EndHead";
<HTML>
<head>
<TITLE>IP details</TITLE>
</head>

<BODY>
<FONT FACE="Arial" SIZE="4" COLOR ="#0000FF">


<!------- HTML HEADER ------------- -->

EndHead
}
#be too big of a change?
#what kinds of changes are you willing to accept?
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10790576
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
0
 
LVL 2

Expert Comment

by:healthstatus
ID: 10792554
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;
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10793649
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?
0
 
LVL 2

Expert Comment

by:healthstatus
ID: 10794990
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.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10795190
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.
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10796658
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.
0
 
LVL 2

Accepted Solution

by:
healthstatus earned 25 total points
ID: 10796816
In your main program very near to the top and before PrintHead ever gets called do this:

use vars ($been_to_printhead);
$been_to_printhead = 0;

then in the PrintHead subroutine:

sub PrintHead {
return if $been_to_printhead;
$been_to_printhead = 1;
(everything else stays the same)
0
 
LVL 1

Author Comment

by:Paulmul
ID: 10797130
healthstats, I'll try that next week. Thanks.
0
 
LVL 1

Author Comment

by:Paulmul
ID: 11974997
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now