Hello Monks
i have the below requirement
have to validate each line of file A to check if it exceeds 70 characters, if yes then truncate from 70th character and put it in next line.
i.e. each line a file should not exceed 70 characters
i have done something like below
#!/usr/bin/perl my $maxlen = 70; my $finalbuffer = undef; print "Check the no of characters in each line \n"; open(FILE,"textfile") or die "Unable to open the textfile \n"; while (<FILE>){ chomp; if ( length($_) > $maxlen ){ my $line = $_; while ( length($line) > $maxlen ){ my $smallchunk = substr($line,0,$maxlen-1); $finalbuffer .= qq~$smallchunk\n~; $line = substr($line,$maxlen-1,length($line)); } $finalbuffer .= qq~$line\n~; } else{ print "Line within range of $maxlen characters \n"; $finalbuffer .= qq~$_\n~; } } close FILE; open(TMP,">tmpfile") or die "Unable to open tmpfile for writing $! \n" +; print TMP $finalbuffer; close TMP;

where textfile contains below string
this line will have more than 70 characters and i should truncate from 70th character and put it in 2nd line so that each line in a file will not be greater than 70 characters which can used to send sms confirmation.

is there any better way to achieve my requirement, i mean just wanted to see if i can get more efficient method by experts.

In reply to Line truncation and validation of no of characters 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.