in reply to output on top of old output

It's the "\b" char if that's the question:
# UNIX style: perl -e '$|++;for(1..10){ print $_;sleep 1; print "\b"}' # dos style: perl -e "$|++;for(1..10){ print $_;sleep 1; print \"\b\"}"


Replies are listed 'Best First'.
Re^2: output on top of old output
by Nkuvu (Priest) on Mar 22, 2005 at 15:43 UTC
    This works fine for single digits, but if you increase the for loop to 15, the output gets all wonky:
    perl -e '$|++;for(1..15){ print $_;sleep 1; print "\b"} # prints: 1111115
    because, of course, you're only backspacing over one character. To avoid extra logic to figure out how many spaces to go back, you can just go to the beginning of the line with \r:
    perl -e '$|++;for(1..15){ print $_;sleep 1; print "\r"}

    Which one is "better" depends on your desired use, obviously.

    Addendum: Ooog, slow typing today.

      Basically, xorl, "\b" backs up one char, while "\r" rewinds the whole line :-)

      Remember, if you print a newline character, you won't be able to use these control characters to erase it.

        I think it'd be just as handy to use their real names: \b == backspace (go back one space), and \r == carriage return (return to the beginning of the line). Originally, these were from the terminal printer days and were just directives to the print head to do exactly that. Carriage returns (returning the carriage to the beginning of the line) were not intrinsically linked to new lines, so we get different characters for these activities.

      ... for(1..15){ print $_;sleep 1; print "\b"x(length$_)} ...


Re^2: output on top of old output
by gellyfish (Monsignor) on Mar 22, 2005 at 15:36 UTC

    Except of course that doesn't work for output of more than one character - try printing 10 - 25 for instance. I guess you could change your "\b" to "\r" if all is required is to return to the beginning of the line.

    /J\

      What does "\r" do on a mac?