You could use a trick I often use, to display a running count, but on the same 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); }
Every time the index variable $i reaches a multiple of $interval (which is 10 in the above program), the line:
(0 == ($i % $interval)) and printf " Progress %5d/%5d\e[K\r", $i, +$total;
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.

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.


In reply to Re: printing a running count by liverpole
in thread printing a running count by phaedo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.