1825792 Members
2537 Online
109687 Solutions
New Discussion

Perl: Formatting Output

 
SOLVED
Go to solution
Michael Selvesteen_2
Trusted Contributor

Perl: Formatting Output

Hello Everybody,

I need to direct the output from a perl subroutine to the screen, but it should not get appended to the previous output whereas it should overwrite the previous.

Simply stating is it possible to replicate the progress meter of sftp.Is there any built-in function or any thought to implement this in perl.

HP-UX 11.11 is my platform and I use puTTY client.

Thanks for your help.

Regards
Michael
2 REPLIES 2
Rodney Hills
Honored Contributor

Re: Perl: Formatting Output

If you are using X-windows can use PerlTK and it has tools for a progress meter.

If you in character mode only, then a typical way to do a progress meter is to display it on a single line starting at the 1st column. For each line displayed, include a "\r" instead of "\n" to force a carriage return (but not new-line). Now you can overwrite the line you are on and re-display the progress meter.

HTH

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl: Formatting Output

my ($edone, $etodo, $enorm) =
$ENV{TERM} eq "xterm" || $ENV{TERM} eq "vt320" ?
map { "\e[${_}m" } qw(0;33;42;1 44 0) :
("", "", "");

sub slide ($$$)
{
my ($i, $n, $x) = @_;

$opt_v or return;

if ($i < 0 || $n <= 0 || $x <= 0) { # Forced reset */
$prv = -1;
printf STDERR "\r";
$slide->(0, 100, 1);
return;
}

$i % $x and return;

my $pct = $i * 100 / $n;
$opt_v > 2 || $pct != $prv or return;

my $str;
$prv = $pct;
if ($opt_v > 1) {
$str = sprintf " %-9d%*d %%%*d ",
$i, $xpct - 10, $pct, $cols - $xpct - 4, $n;
}
else {
$str = sprintf "%*d %%%*s", $xpct, $pct, $cols - $xpct - 3, "";
}
$pct > 100 and $pct = 100;
my $a = ($cols - 1) * $pct / 100;
printf STDERR "\r%s%.*s%s%.*s%s\r",
$edone, $a, $str, $etodo, $cols - 1 - $a, substr ($str, $a), $enorm;
} # slide

my $last = 10000;
foreach my $i (1 .. $last) {
slide ($i, $last, 100);
}

Something like that?

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn