Solved

Loops in Perl

Posted on 2004-04-07
26
355 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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
 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automating a script for user accounts LINUX 14 77
Set robocopy to exclude "System Volume Information" folder 3 198
Review of the nodejs code 1 35
Deploy restore partition 1 23
In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

713 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