LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl - printing progress on same line. (https://www.linuxquestions.org/questions/programming-9/perl-printing-progress-on-same-line-335818/)

Technoslave 06-21-2005 01:50 PM

Perl - printing progress on same line.
 
I have a script that prints out an iteration of numbers. The problem is the printed output scrolls. Exa:

123211 packets sent.
123212 packets sent.
123213 packets sent.
123214 packets sent.
123215 packets sent.

Simple code exa:

$iter++;
print "$iter packets sent.\n" if $verbose;

****

What I want instead is for only a single line to be printed but the number on that single line to increment ( no scrolling ). I've seen this before but can't find my example, so hoping someone can help me and/or point me to a web page that might have further info on it.

Thanks,

rjlee 06-21-2005 03:21 PM

\n will take the cursor on to the next line, scrolling as necessary
\r will take you to the start of the current line:
Code:

$iter++;
print "$iter packets sent.\r" if $verbose;


Technoslave 06-21-2005 03:40 PM

That's what I was looking for. Thanks!

Flimm 08-05-2011 09:00 AM

This doesn't work as expected if the line being printed is longer than the width of the terminal.

Tinkster 08-05-2011 09:53 PM

Quote:

Originally Posted by Flimm (Post 4434533)
This doesn't work as expected if the line being printed is longer than the width of the terminal.

Since you chose to unearth a 6 years dead thread ... why don't
you post a solution to the THAT problem?



Cheers,
Tink

theNbomr 08-07-2011 12:35 PM

Since we seem to be getting a lot of replies to zombie threads lately, perhaps it is appropriate to mention that the Zero-Reply Threads pseudo-forum exists. It almost guarantees that the threads founds are current.
--- rod.

Flimm 09-03-2011 01:21 PM

There is no real solution that I know of.

You can use GetTerminalSize like this:

use Term::ReadKey; ($width) = GetTerminalSize(); # don't forget the parens round $width

and then only print out out the first $width characters of a string. You can't print something out over two lines and then overwrite both lines.

sundialsvcs 09-03-2011 09:20 PM

There is a Perl variable ... see perldoc perlvar and look up "$|" ... that's dollar-sign vertical-bar.

This controls whether output is "buffered" pending the next newline character.


All times are GMT -5. The time now is 09:20 AM.