Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-Write line 1

Status
Not open for further replies.

CJason

Programmer
Joined
Oct 13, 2004
Messages
223
Location
US
I want to re-write a line that has been output to the screen via a print statement. Basically, I want to make a countdown-counter...something like:

Remaining: 350

and just change the "350" to "349", etc...

Any ideas?

Thanks in advance!
 
Take advantage of the "\r" character.

Code:
local $| = 1; # Flush STDOUT
for (my $i = 350; $i >= 1; $i--) {
  print "\rRemaining: $i  "; # Note extra padding for when number of digits reduces.
  sleep 1;
}
 
hehehe, I think I did this in class decades ago:

Code:
print "Shutting down in:\n"; 
$|++;
for (reverse (0..20)) {
  if ($_ < 10) {
     print "\r*** DANGER! ALL DATA WILL BE ERASED IN $_ SECONDS! PRESS Ctrl^C TO ABORT ***";
  }
  else {
     printf "\r%d ",$_;
  }
  sleep 1;
}

- Kevin, perl coder unexceptional!
 
Thanks guys...those work like a charm! For some reason, I can't find that "\r" option anywhere in my Perl book!?!?!?
 
CJason said:
Thanks guys...those work like a charm! For some reason, I can't find that "\r" option anywhere in my Perl book!?!?!?

It's indexed under "carriage returns". It's not really a perl thing, more of a unix or general computer knowledge thing.
 
Hello MillerH and Kevin so this can be used to rewrite a line with a file can this also be use to update the file as
like I would try to use it for members login and when they logout use it to remove the name?

MA WarGod

I believe if someone can think it, it can be programmed
 
Short answer is: no.

- Kevin, perl coder unexceptional!
 
\r is Carriage Return (CR). Think back to the days of teletypes, and you'll get the idea. On a console, it returns the cursor to the beginning of the line without doing a line feed (LF), so you effectively overwrite what's already on the screen (which is why MillerH's solution pads the numerics on the right to erase the old characters when it goes from 100 to 99 and 10 to 9).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Steve
Thank You for explaining that I truely would not of understood less it was told that way

MA WarGod

I believe if someone can think it, it can be programmed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top