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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |