in reply to Printing to stdout an array of strings and scalar references

I agree with advice that a template is a good idea, even with a command line tool, but there is already a nice progress package specifically for command line tools: Time::Progress.

#!/usr/bin/env perl use strictures; use Time::Progress; use Time::HiRes "usleep"; # <- just for demo. my $total = 42; my $progress = Time::Progress->new(); $progress->attr( min => 1, max => $total ); $| = 1; for my $done ( 1 .. $total ) { usleep( rand 500_000 ); print $progress->report("\rProcessing: $total files to process %L +%40B%E %p", $done); } print "\nDone!\n";

Update: fixed code booger; s/42/\$total/ in for loop.

Replies are listed 'Best First'.
Re^2: Printing to stdout an array of strings and scalar references
by stevieb (Canon) on Oct 25, 2017 at 17:14 UTC

    Well, I can say I learned something today... I had no idea that you could separate integers with underscores to make it easier at a glance how long the number actually is.

    perl -E 'say 500_000' 500000

    Cool!

      You can use underscores with decimal, hexadecimal, octal and binary numbers. You can also place them wherever you want, for convenient viewing of the data in question. They don't have to be a direct replacement of commas: both 50_00_00 and 500_000 are valid representations of 500,000; although, the first one is probably a poor choice because it's likely to be confusing.

      Here's some highly contrived examples (purely to demonstrate those points):

      $ perl -E 'say 1_6' 16 $ perl -E 'say 0x1_0' 16 $ perl -E 'say 02_0' 16 $ perl -E 'say 0b1_00_00' 16

      See "perldata: Scalar value constructors" for details.

      — Ken

        both 50_00_00 and 500_000 are valid representations of 500,000; although, the first one is probably a poor choice because it's likely to be confusing.

        Not everyone on earth seperates digits in groups of three. In India, it is common to use groups of two and groups of three, mixed: Indian numbering system.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Yes, I'm also a fan of Perl allowing underscores in numeric literals.

      BTW, I was pleasantly surprised to learn that the C++ committee are also fans of this splendid idea:

      • C++11 user-defined literals: These are extensible and use a trailing underscore. For example: 12_km
      • C++14 digit separators: "The single-quote character ' can now be used anywhere within a numeric literal for aesthetic readability". For example: 500'000 or 500'000_km or pi = 3.14159'26535'89793. Presumably, single quote was chosen for the digit separator because underscore had already been taken by user-defined literals.

      In fact, it's a perlism I am extremely fond of. I can easily see someone in a design discussion saying, What? We don't need that informal, random parsing trouble. It's just numbers. If you can't read numbers, you shouldn't be programming. That underscore has saved me many hours of reading comprehension and probably a hundred hours of chasing bugs and mistakes in code. Update: and scratches on my monitor as I try to count digits with pen or ruler. :P

Re^2: Printing to stdout an array of strings and scalar references
by Anonymous Monk on Oct 30, 2017 at 00:34 UTC

    Thanks for the advice about Time::Progress, That module has proven to be very useful.