Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"
 
PerlMonks  

Re: scalar or sub that can tell me the terminal print offset ?

by Not_a_Number (Parson)
on Mar 17, 2012 at 18:43 UTC ( #960199=note: print w/ replies, xml ) Need Help??


in reply to scalar or sub that can tell me the terminal print offset ?

Not sure what you're actually looking for.

Would this fit the bill?

my @strings = ( 'Paint it black', 'Satisfaction', 'Lady Jane' ) ; say $_, ' ', length for @strings;


Comment on Re: scalar or sub that can tell me the terminal print offset ?
Download Code
Re^2: scalar or sub that can tell me the terminal print offset ?
by palkia (Scribe) on Mar 17, 2012 at 19:24 UTC
    Thx, but no it won't.
    This is how I expect such a scalar (or sub) to work:
    1. Say I print a 5 (normal) chars string, than the desired scalar will have a increase in value of 5.
    2. when in the beginning of a line it will be zero.
    3. after a print of \n or \r, reset to zero.
    4. after a print of \t, increase value to the next multiple of 8.
    5. after a print of \b, decrease value by 1.
    6. after a print of \a, no change in value.
    7. modulus: every time it gets over 80 it is reduced by 80 (after 80 chars there's a new line).

    Probably some other rules too.
    I can figure these out, so I can write it (probably),
    but I find it hard to believe this doesn't already exists.
      >but I find it hard to believe this doesn't already exists.

      most probably this is just an xy problem. i.e. you are trying to solve a problem in a wrong way.

      I slightly remember a BASIC dialect which had such a feature to allow positioning the cursor, but in Perl I never missed this.

      IMHO in reality you'd rather prefer using

    • printf or
    • format or
    • templates with here-docs or
    • Term::ReadLine or
    • GUI-library like Tk

      (just some plausible guesses).

      If all of this is wrong and you don't wanna give us more insights, you can still try writing a routine out() to replace your prints, which automatically splits the arguments at newlines and counts the length of the last line in a global var $OUTPOS.

      Cheers Rolf

        Not_a_Number hits it squarely on the head, as best /me can tell from the question and addendum.

        "Text::Wrap" should be fairly easy to modify to suit your rules -- once you've decided what all of them are.

        But if you're a glutton for punishment, you could modify this exercise (which dates back several years and is rather fragile -- tabs in the data, for example, would break the formatting for the lines in which they appeared) -- likely by outputting the $chars to another string, and reporting its len.

        #!/usr/bin/perl use strict; use warnings; my ($content, @content); { local $/; $content = <DATA>; } @content = split //, $content; my $content_ref = \@content; count( $content_ref ); sub count { print " 1 2 3 4 5 + 6\n"; print "12345678901234567890012345678901234567890123456789012345678 +90\n"; my $char; my $count = 0; my $string_ref = shift; my @string = @$string_ref; for $char ( @string ) { if ( ( $count > 50 ) && ( $char ne ' ' ) ) { print $char; ++$count; next; }elsif ( ( $count > 50 ) && ( $char eq ' ' ) ) { print "$char\n"; $count = 0; next; } elsif ( $char =~ /\n/ ) { print "$char"; $count = 0; next; } else { print $char; ++$count; } } } __DATA__ data: This is a paragraph that will be far too wide to print on a single lin +e in a normal width terminal (console) so we are going to count chars + and tell Perl to insert a newline after a reasonable number of chars +, sixty, in this case. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent ant +e metus, iaculis a, adipiscing nec, consequat et, arcu. Proin massa. +Nulla dictum, leo ac elementum scelerisque, felis ipsum porta justo, +a viverra orci sem ut diam. Integer facilisis volutpat orci. Donec at + turpis sed purus ultricies rhoncus. Fusce urna. Praesent ac ligula i +d augue interdum consectetuer. Sed vestibulum, dui eu dignissim tinci +dunt, mi orci eleifend metus, tempus placerat arcu nulla sit amet lac +us. Duis felis. Aenean lobortis turpis a elit. Suspendisse mi quam, t +empus ac, venenatis in, commodo sed, enim. Proin dignissim placerat l +acus. Maecenas blandit est et ipsum. In dignissim suscipit leo. Ut congue mi + nec dolor. Curabitur malesuada purus congue purus. Nulla facilisi. M +aecenas at felis. Aliquam cursus, risus vitae rutrum vestibulum, tell +us ante pulvinar tortor, rutrum faucibus enim magna ac nisi. Proin li +bero mauris, aliquam vel, luctus vitae, pretium eget, nisi. Etiam nun +c neque, tempus malesuada, commodo non, tempor vel, sapien. Duis elem +entum fermentum libero. In tristique. Curabitur pede neque, adipiscin +g quis, egestas a, imperdiet ac, velit.
        Output:
        1 2 3 4 5 6 1234567890123456789001234567890123456789012345678901234567890 data: This is a paragraph that will be far too wide to print on a single line in a normal width terminal (console) so we are going to count chars and tell Perl to insert a newline after a reasonable number of chars, sixty, in this case. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent ante metus, iaculis a, adipiscing nec, consequat et, arcu. Proin massa. Nulla dictum, leo ac elementum scelerisque, felis ipsum porta justo, a viverra orci sem ut diam. Integer facilisis volutpat orci. Donec at turpis sed purus ultricies rhoncus. Fusce urna. Praesent ac ligula id augue interdum consectetuer. Sed vestibulum, dui eu dignissim tincidunt, mi orci eleifend metus, tempus placerat arcu nulla sit amet lacus. Duis felis. Aenean lobortis turpis a elit. Suspendisse mi quam, tempus ac, venenatis in, commodo sed, enim. Proin dignissim placerat lacus. Maecenas blandit est et ipsum. In dignissim suscipit leo. Ut congue mi nec dolor. Curabitur malesuada purus congue purus. Nulla facilisi. Maecenas at felis. Aliquam cursus, risus vitae rutrum vestibulum, tellus ante pulvinar tortor, rutrum faucibus enim magna ac nisi. Proin libero mauris, aliquam vel, luctus vitae, pretium eget, nisi. Etiam nunc neque, tempus malesuada, commodo non, tempor vel, sapien. Duis elementum fermentum libero. In tristique. Curabitur pede neque, adipiscing quis, egestas a, imperdiet ac, velit.

        But plaudits to those others who also suspect an XY problem, too, as what you've specced so far, seems somewhat removed from real-world relevance. Or, is this homework? If so, say so, because you learn very little about programming if you hand in one of our solutions.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-04-21 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls