473,796 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STDOUT - Overwriting previous line

15 New Member
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 11082
miller
1,089 Recognized Expert Top Contributor
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
docsnyder
88 New Member
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
4670
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
2
3855
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 "received world". Instead I get nothing for 5 seconds and then both statements pop out at once. As you'll no doubt gather from the example, the problem I'm really trying to solve is sending message back from one process to another via a pipe....
2
1852
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
9585
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
5388
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
4
6106
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
2
5072
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) on linux2 1) Does " >>>print 'hello' " simply write to sys.stdout?
11
5529
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 previous two. The sleeping bit is trivial but, what about the overwriting? I am assuming that the output will be displayed on a standard terminal emulator, like xterm.
0
2442
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 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait for the process to return
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.