For my Unix class last semester, Perl was briefly introuced and I decided that the assignments were better handled using Perl than Bash. When time came for the final, the extra credit involved figuring out if a number was prime or not, either by calculating it, by looking it up, or both.

I've taken that concept and to try to get my feet wet in Perl, I decided to expand upon it.

My latest script asks for a lower- and upper-bound for calculating, then determines all the primes and outputs it to a file.

I've had a spinner in there previously to show it was working; right now it displays how many primes have been found in X seconds, but I'd like to use a progress bar to show how far along the script is.

I couldn't get Term::ProgressBar to work for me, so I switched to Term::StatusBar.

What I want to have (ideally) is one line showing what I already have (the # of primes found and the elapsed time) and below it, a progress bar. Right now, it's displaying the bar but it stays at 0% and never moves.

#! /usr/bin/perl use Time::HiRes qw(time); use Math::Round 'nlowmult'; use Term::StatusBar; print "Enter the lower bound:\n"; chomp ($low=<>); while ($low < 1) { print "That number is not valid.\n"; print "Enter the lower bound:\n"; chomp ($low = <>); } print "Enter the upper bound:\n"; chomp ($high = <>); while ($high <= $low) { print "That number is not valid.\n"; print "Enter the upper bound:\n"; chomp ($high = <>); } $totalnumber = $high - $low; my $status = new Term::StatusBar ( label => 'Progress: ', totalItems => $totalnumber, startRow => 3, ); system (clear); print "Finding the primes between $low and $high...\n"; $status->start; $total = 0; $timestart = time; open (my $fh, ">>", primes); print $fh "#:Time(s):Result\n"; print $fh "============================\n"; close $fh; for ($i=$low; $i<=$high; $i++) { $is_prime = 1; for ($j=2; $j<=sqrt($i); $j++) { if ($i%$j == 0) { $is_prime = 0; $status->setItems($i); for (1..$_[0]){ sleep 1; $status->update; } break; } } if ($is_prime == 1) { $total++; open (my $fh, ">>", primes); $duration = time - $timestart; print $fh "$total:$duration:$i\n"; close $fh; $duration = nlowmult(0.01, time - $timestart); # print "\rFound $total primes so far in $duration seconds..."; } } print "\n";

Not the most elegant, I'll grant, but I literally started learning this language a month ago. And also, I know there's better ways to determine primes. Again, this is for my own edification. What am I doing wrong here?

Thanks in advance.


In reply to Using a progress bar... having issues by TitoLasVegas

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.