473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

STDOUT - Overwriting previous line

15
Hello,
I am trying to update the STDOUT dynamically.
For eg. I am trying to display a counter whose value should get updated int he same place,

#!/usr/bin/perl

$val = 10;
for ($i=0;$i<=$val;$i+) {
print "$val\n";
}
would print
0
1
2
3
4
5
6
7
8
9
10

What I am looking at is a clock like feature where the earlier output is overwritten by the current value of val.
Feb 17 '07 #1
2 11035
miller
1,089 Expert 1GB
Read this thread for discussion on flushing STDOUT
http://www.thescripts.com/forum/thread603712.html

As far as overwriting the same line, you simply need a carriage return: "\r". You can read about them in the O'Reilly Camel book under "carriage returns".

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5.  
  6. local $| = 1;
  7.  
  8. my $val = 10;
  9. for (my $i=0; $i<=$val; $i++) {
  10.     print "\r$val";
  11. }
  12.  
Feb 17 '07 #2
Consider a user wants to write to the same line, where subsequent lines could get shorter. The example (according to youre approach):
Expand|Select|Wrap|Line Numbers
  1. my $val = 0;
  2. for (my $i=10; $i>$val; $i--) {
  3.     print "\r$i";
  4.     sleep(1);
  5. }
would print
Expand|Select|Wrap|Line Numbers
  1. 10
  2. 90
  3. 80
  4. ...
  5.  
A solution to erase everything written before would be:
Expand|Select|Wrap|Line Numbers
  1. my $val = 0;
  2. my $n   = 0;
  3. for (my $i=10; $i>$val; $i--) {
  4.     printf("\r%s\r$i", " " x $n);
  5.     $n = length($i."");
  6. }
Greetz, Doc
Feb 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
2
by: Graham Ashton | last post by:
Hi. I'm having trouble flushing sys.stdout. I've written a small example to illustrate my problem (see below). In short, I expect it to ping "received hello", sleep for 5 seconds and then print...
2
by: Leon | last post by:
import os,sys a = os.fdopen(sys.stdout.fileno(),"w") b = os.fdopen(sys.stdout.fileno(),"w") a.write("test1 ") b.write("test2 ") stdout result is test1 test2
5
by: Joseph | last post by:
Hi all, I am doing my assignment and have a question ,how can I clear the standard output(screen)? following is the fake code: ===================================== main(){
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
11
by: Steven Jones | last post by:
I have a C program that prints out two lines as follows: Line 1 Line 2 What I would like is for this program to sleep for one second, and then print out two more lines, overwriting the...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.