Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Typeover output

by L8on (Acolyte)
on Jan 12, 2005 at 21:51 UTC ( [id://421794]=perlquestion: print w/replies, xml ) Need Help??

L8on has asked for the wisdom of the Perl Monks concerning the following question:

I have been wondering about this off & on for quite a while
and something today made me think of it again, so I'll ask this time.

Is there anyway to overwrite a line of output on a console with a new line of text?

Say, for example, if I was walking through a file one line at a time
and wanted to output a statement of the current line number I would use the following:

while (.......){ #read line x print ("processing line: $linenum "); }
For each line of the above, the output would output:
processing line: 1 2 3 4 5 6 7 8 ....... x
What I was curious about, is there a way to just replace the line so that only $linenum changes on output?
( e.g. 1 changes to 2 and so on )

Thanx in advance.
l8on

Replies are listed 'Best First'.
Re: Typeover output
by Roy Johnson (Monsignor) on Jan 12, 2005 at 22:11 UTC
    Try this:
    $|=1; print "Processing line: "; for (8..15) { print $_ . "\b" x length; sleep 1; } print "\nDone\n";

    Caution: Contents may have been coded under pressure.
Re: Typeover output
by gaal (Parson) on Jan 12, 2005 at 22:13 UTC
    perl -e '$|++; print "I like moose"; sleep 1; print "\rI like elk"'

    The trick is to send a carrige return to the terminal. But this may not work everywhere; YMMV.

Re: Typeover output
by gopalr (Priest) on Jan 13, 2005 at 02:33 UTC

    Hi,

    Use Carriage return '\r'

    while (.......){ #read line x print ("processing line: $linenum \r"); }

    Thanks,

    Gopal.R

Re: Typeover output
by jonadab (Parson) on Jan 12, 2005 at 22:24 UTC
    I believe one of the various Term:: modules may handle this sort of thing, but I don't recall which, as it's not something I've ever actively used much. There was one discussed on the 2004 Perl Advent Calendar for printing progress bars, which is not quite what you want, but you could look at how it achieves that effect and do the same thing.
Re: Typeover output
by gube (Parson) on Jan 13, 2005 at 03:53 UTC

    Try this,

    $str = "hello"; print "$str"; $str = "hai"; print "\r$str";

    Use Escape sequence carriage return

    o/p: hailo

    Regards,

    Gubendran

Re: Typeover output
by YuckFoo (Abbot) on Jan 13, 2005 at 21:36 UTC
    l8on,

    Another trick is to use the modulus operator (%) to only print every 100th, 1000th, etc., line number:

    YuckFoo

    #!/usr/bin/perl $|++; for my $i (0..10000000) { $i % 100000 or print "line: $i\r"; }
      Thanx everyone. I knew that I should've been able to do it, but couldn't figure it out.

      And to quote a classic:
      "Now I know, and knowing.... is half the battle."


      Thanx again, L8on

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://421794]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found