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

hii

i have the following code :
 printf("%-30s","0");
for($i=0;$i<10;$i++)
{
 print "\e[30D";
 printf("%-30s","$i");
 sleep(1);
}

I want it to print 0 then wait for 1 second erase it
then print 1 wait for second erase it and print 2 and so on...

But only the 9 is being printed !
what am i doing wrong???
_____________________________________
Perl 5.8.8
Linux Fedora Core 6
_____________________________________
http://digitalpbk.blogspot.com

Replies are listed 'Best First'.
Re: Problem with print
by RL (Monk) on May 10, 2007 at 05:20 UTC

    Does it help to turn autoflush on?

    $|++;

      RL has nailed it. Compare,

      $ perl -e'sleep 1, print "\r$_" for 1..10; print $/'
      to,
      $ perl -e'$| = 1; sleep 1, print "\r$_" for 1..10; print $/'

      After Compline,
      Zaxo

        thnx guys solved it adding this solves it autoflush STDOUT 1;
Re: Problem with print
by GrandFather (Saint) on May 10, 2007 at 05:03 UTC

    On my (non-ANSI) terminal I see the 0 printed, then the escape string followed by a digit for the digits 1 .. 9 at one second intervals. Are you sure you have the escape string right? How much delay from start until the 9 appears? Does it work if you omit the escape string (that is, just print the digits at one second intervals)?

    You seem to be learning Perl. I'd strongly recommend that you use strictures (use strict; use warnings;). You should also use a more Perlish for loop to reduce potential coding errors and make the code easier to read:

    for my $i (0 .. 9) { ... }

    DWIM is Perl's answer to Gödel
Re: Problem with print
by halley (Prior) on May 10, 2007 at 17:29 UTC
    You're suffering from buffering. If you want to do something like this, but not depend on ANSI terminal control codes, you might try to use just "\r" characters on its own line. See my peek - output a one-line preview of stdin writeup for more.

    --
    [ e d @ h a l l e y . c c ]