in reply to printing a running count
Every time the index variable $i reaches a multiple of $interval (which is 10 in the above program), the line:#!/usr/bin/perl -w + # Strict use strict; use warnings; + + # User-defined my $interval = 10; + + # Main program $| = 1; # Flush STDOUT my $i; my $total = 10_000; + for ($i = 0; $i < $total; $i++) { (0 == ($i % $interval)) and printf " Progress %5d/%5d\e[K\r", $i, +$total; # Call your code here &my_subroutine(); } printf " Progress %5d/%5d\e[K\n", $i, $total; + + # Subroutines sub my_subroutine() { # For test purposes, sleep for a random interval of milliseconds my $nmilli = int(rand(1100) / 1000); select(undef, undef, undef, $nmilli); }
causes a count to be displayed, along with the total. The escape sequence "\e[K" erases to the end of the line, and the carriage-return "\r" repositions the cursor at the beginning of the line (which is why I recommend pussing the extra space before the word "Progress"). At the end of the loop, don't forget to rewrite the progress, but this time with "\n" to finally go to a new line.(0 == ($i % $interval)) and printf " Progress %5d/%5d\e[K\r", $i, +$total;
You can change the output to your liking; from displaying only the count (with no label) to displaying the percentage complete (eg. printf " Progress %7.3f%%", 100 * $i / $total;"), etc.
|
|---|