Professional Web Applications Themes

Using print to overwrite a line - PERL Beginners

Is there a simple way to print over an existing line? For example: I would like to have a progress report that reads 10%, 20% etc. I don't want to print a new line each time. I want to write over the existing line and have it update as it goes. --Paul...

  1. #1

    Default Using print to overwrite a line

    Is there a simple way to print over an existing line?

    For example: I would like to have a progress report that reads 10%, 20%
    etc. I don't want to print a new line each time. I want to write over
    the existing line and have it update as it goes.

    --Paul

    Paul Harwood Guest

  2. #2

    Default RE: Using print to overwrite a line

    Paul Harwood wrote:
    > Is there a simple way to print over an existing line?
    >
    > For example: I would like to have a progress report that
    > reads 10%, 20%
    > etc. I don't want to print a new line each time. I want to write over
    > the existing line and have it update as it goes.
    >
    > --Paul
    Use "\r" to move to the beginning of the current line (depending on the
    capabilities of your terminal). Also unbuffer STDOUT. Something like this:

    $| = 1;
    for (1 .. 5) {
    print "\rStep $_";
    sleep 1;
    }
    print "\rFinished.\n";

    Bob Showalter Guest

  3. #3

    Default Re: Using print to overwrite a line

    Paul Harwood wrote:
    >
    > Is there a simple way to print over an existing line?
    >
    > For example: I would like to have a progress report that reads 10%, 20%
    > etc. I don't want to print a new line each time. I want to write over
    > the existing line and have it update as it goes.
    Hi Paul.

    I presume you're writing to a file? Can you show us an example of what
    you mean? It's probably best if you rewrite the file, but that's not
    a problem.

    Rob


    Rob Dixon Guest

  4. #4

    Default Re: Using print to overwrite a line

    This is not the most elegant solution, but it did only take me five minutes.
    The salient part is the backspace and printing blanks bit. A more elegent
    solution would figure out how to destructively clear the line using something
    from ncurses.

    #!/usr/bin/perl -w

    $|=1;

    reports = ("starting", "now we're cooking", "Almost half!", "Over the hump",
    "Almost done!", "Finito!");
    #print reports;

    foreach $item (reports)
    {
    print $item;

    sleep 1;
    print "\b" x length($item);
    print ' ' x length($item);
    print "\b" x length($item);

    }

    --
    Music so wishes to be heard that it calls on some to give it voice and some to
    give it ears.
    [url]http://www.hacksaw.org[/url] -- [url]http://www.privatecircus.com[/url] -- KB1FVD


    Hacksaw Guest

  5. #5

    Default Re: Using print to overwrite a line

    This doesn't work if the line being printed is longer than the width of the terminal.
    Flimm Guest

Similar Threads

  1. Question Use Command line to set Print Scaling with a PDF
    By djenkins in forum Brainstorming Area
    Replies: 0
    Last Post: December 3rd, 10:15 PM
  2. In-line graphics won't print
    By solid1 in forum Macromedia Freehand
    Replies: 2
    Last Post: July 21st, 03:48 PM
  3. Windows Print PDF from Command Line
    By Lorraine_Siegmann@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 3
    Last Post: April 6th, 08:49 PM
  4. 'print' output on one line
    By Anthony Esposito in forum PERL Beginners
    Replies: 7
    Last Post: January 6th, 11:36 PM
  5. printer in jetdirect only print one line...
    By Shiva MahaDeva in forum Sun Solaris
    Replies: 0
    Last Post: June 24th, 11:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139