> -----Original Message-----
> From: Tyler Longren [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, August 15, 2001 10:13 AM
> To: Perl-Beginners
> Subject: status messages as the program progresses
> 
> 
> Hello everyone,
> 
> I have a while loop that loops through a text file for quite 
> a while (10
> min or so).  Is there any way to print a status-type message 
> while it's
> looping through?
> I did something like this now:
> 
> my $i=0;
> while ($i le "10000000") {
>       print "Processing\r";
> }

I hope that's not the whole script, since $i doesn't change :)

> 
> And that works, but if I run the script like this:
> ./script.pl > test.txt
> 
> I get a BUNCH of "Processing" lines in test.txt.  Is there 
> any other way I
> can do this?

One thing you can do is print the message every so many lines.

   my $i = 0;
   while (<>)
   {
       # (...process line here...)
       print "$i lines processed\n" unless ++$i % 100;
   }

This will print:

   100 lines processed
   200 lines processed
   300 lines processed

and so on.

Or, you can print the message every so many seconds:

   my $last_time = time;
   my $i = 0;
   while (<>)
   {
       # (...process line here...)
       $i++;
       print("$i lines processed\n"), $last_time = time if time - $last_time
> 30;
   }

This uses the variable $last_time to keep track of when a message was last
printed. If this has been more than 30 seconds, a line is printed and the
$last_time variable is updated.

These methods assume each iteration of the loop is relatively quick. If the
processing for a single line can be lengthy, you can use a $SIG{ALRM}
handler
to cause an interrupt in the middle of processing. However, it's generally
not a good idea to do I/O during an event handler, so you usually set a flag
that you check somewhere inside your processing loop.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to