Hello Monks
i do have a requirement as below.
got a string having n number of characters , i want to break that string into 2 lines with each line not exceeding more than 60 characters.
for which i have done below.
#!/usr/bin/perl use strict; use warnings; my $maxlen = 60; my $finalbuffer = undef; print "Check the no of characters in each smstext \n"; die "Pls supply SMS Text as Argument\n" if ( $#ARGV < 0 ); my $smstext = $ARGV[0]; if ( length($smstext) > $maxlen ){ $finalbuffer=subdivide($smstext,60); } print $finalbuffer,"\n"; # This will divide into chunks of 70 characters each sub subdivide { my ($s, $n) = @_ ; $s =~ s/\G(.{$n})(?!\Z)/$1\n/g ; return $s ; } ; # Another method print join "\n", unpack("(a59)*", $smstext),"\n";

we can call the code as below
ersms.pl "This sentence will have more than 120 characters and i want to truncate this string into two lines containing 60 characters each and ignore characters above 140 in length"
my problem is, by above methods i can cut the line to 60 characters, but sometimes line end is the mid of some word which is not intended.
i need line to have 60 characters with word ending properly otherwise if if needed that word to move into next line i.e. each line should with word boundary and should not exceed 60 characters
pls help

In reply to Breaking String by harishnuti

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.