Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

overwritting print in same line

by sagar123 (Novice)
on Nov 14, 2011 at 05:51 UTC ( #937870=perlquestion: print w/replies, xml ) Need Help??

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

I want to print dots ....... just to make user know that job is still running, here is something in perl I am copying, but it prints dots in different lines. Is it possible to over write .... dots so that it dont fill the terminal

while (1) { for (1..20) {print "."; system ("sleep 0.05"); } print "\n"; }

Replies are listed 'Best First'.
Re: overwritting print in same line
by quester (Vicar) on Nov 14, 2011 at 06:30 UTC
    I'm not entirely sure that I understand your question, but if you just want to show that the script is running without using up screen space, consider using a "spinner" which twirls in place:
    use warnings; use strict; my $spin; while (1) { print substr( "-/|\\", $spin++ % 4, 1 ), "\b"; $| = 1; select undef, undef, undef, 0.05; }
    I changed your "sleep .05" to "select undef, undef, undef, 0.05" because the normal Perl sleep function works in seconds, so "sleep .05" is the same as "sleep 0". See sleep for an explanation and other alternatives. I also added "$|=1" to avoid having the output getting buffered and not displaying anything for long periods of time; see $OUTPUT_AUTOFLUSH.

      Cool spinner code. Thanks for sharing. Fun.

      Btw, The autoflush  $| = 1; can actually be put outside the while loop (above it, obviously), as it's a flag not a function in and of itself and so it doesn't require being repeatedly called for each spin segment...

Re: overwritting print in same line
by tweetiepooh (Hermit) on Nov 14, 2011 at 16:35 UTC
    If you just want to add dots then combine switching off buffering output with not printing "\n"
    $|=1; [some loop condition] print "."; [do other stuff] [end loop]
    This will print dots next to each other which maybe close to what you are asking for.
    Depending on what you are doing you may only want to print a dot every n iterations
    $|=1; $counter=0; [some loop condition] print "." unless $count++ % n; [do other stuff] [end loop]
Re: overwritting print in same line
by ansh batra (Friar) on Nov 14, 2011 at 06:48 UTC
    i know its not the correct solution
    just came to my mind so wanted to share
    while (1) { for (1..20) {print "."; system ("sleep 0.05"); } #print "\n"; system("clear"); }

      hi ansh, quester and anonymous thanks for the replies

      ansh: u suggested is tricky but it actually clear off the screen that what dont want to do that. As u can see spinner suggested by quester is quite good in the sense it doesnot clear off the screen. So if u just replace \b with \n it will print in separate lines. But with \b it is printing on same line.

      quester: this is what I wanted. But ethically i cant use ur code so I need to solve my problem. for example in ur code u replace \b with \n it print in separate line. This is what happening with my dots. But if u keep \b in ur code it prints -\/ in same line and "overwriting" it at same place.I dont understand ur code well. ALSO please can u explain ur code explicitly as exactly whats happening. I am somewhat slow :-(

      Thanks to anonymous also

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-10-03 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (15 votes). Check out past polls.

    Notices?