Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re^4: scalar or sub that can tell me the terminal print offset ? by ww
in thread scalar or sub that can tell me the terminal print offset ? by palkia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (8)
    As of 2014-04-17 05:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (439 votes), past polls