Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl (not cgi's) progress

Status
Not open for further replies.

NEVERSLEEP

Programmer
Apr 14, 2002
667
CA
lets say u have a program
than does x time a operation
how can show i progress (like " 12 % done " etc..)
without doing something like :
Code:
&do_operation;
print "1 % done";
&do_operation;
print "2 % done";
etc...
cause this could b false (say the operation are not the same)

thanks a lot ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
If you know at runtime how large an operation would be, divide by your update division (like 100 for single percent increases).
Code:
$num_repitions = 3000;
$division = $num_repitions / 100;
$progress = 0;
print "$progress \% done\n";

for($i=1;$i<=$num_repitions;$i++)
{
    #do work of whatever kind
    update_progress() if($i % $division == 0);
}

sub update_progress()
{
    $progress++;
    print &quot;$progress \% done\n&quot;;
}
Pretty rusty, but something to that effect might work. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
that a good idea but the operation are not always the same
lets say it read a xxxmb file then
write a xxxmb file
then write a xxmb file
then defrag drive c (run a exe)
then runs a batch
then runs another perl script
then....
u get the point [lol]

also is there another way to print progress dynamicly
without using system() ?
Code:
sub update_progress()
{
    $progress++;
    system('cls');
    print &quot;$progress \% done\n&quot;;
}
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Well, for any progress bar to work, it would have to update every so often, so you'd have to go through the same process for every operation. There's no way for anything to just &quot;know&quot; how far along any given operation is.

For processing large files, you could use the stat function to determine filesize and base it off that if the operation scales linearly.

For defraging or other such operations...I think that's rather hopeless. The system command doesn't return until the called program has completed. Just use defrag's own output as the progress indicator, or I suppose you could try and pipe its output back into the perl script and deal with it there...not real sure how that works, I can vaugely recall reading that somewhere.

For calling a clear screen, the ascii character 8 is a backspace (I'd imagine it's the same in unicode), you could output however many of these needed to back over your text. The backspace does not remove newlines, so don't tack one of those onto the %out.
Code:
sub backup
{
	my($count,$i);
	$count = shift;
	for($i=0;$i<$count;$i++)
	{
		print chr(8);
	}
}
Use backup(5) to backspace 5 characters. Would probably be best if you would back up over the entirety of your progress output. Just in case something comes up in the program and something else gets outputted, you would reprint the entire progress line every time. And if you need to output something during the operation, be sure to end it with a newline so the backup function won't back up over it. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
why not have a progress bar, or whatever, for each job? Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top