I didn't see your answer before I typed this, but it's basically the same thing as your if statement, just more descriptive than it needs to be and it uses if-return structure rather than if/elsif/else, yours being more efficient.
I think I'd rather the method you posted, but would store length($2) in a variable, since that is the only one that may be called twice, and depending on the string, it could be the longest, so it'd save the most cpu. -- this depends on how often the second if-block would be triggered
#/usr/bin/perl use strict; use constant TRUE => 1; use constant FALSE => 0; our $debug = FALSE; print truncate_string(" abcdefg ",256); sub truncate_string{ my ($string,$trunc_length) = @_; $string =~ m/^(\s*)(.*?)(\s*)$/; my $space_count_pre = length($1); my $letter_count = length($2); my $space_text_length = $space_count_pre + $letter_count; sub debug{ print "Beginning Spaces: $space_count_pre\n"; print "Text Length: $letter_count\n"; print "Trunc Length: $trunc_length\n"; print "String: \"$string\"\n\n"; } debug() if $debug; # if the final string is already less than the current number of + alphanumeric characters if ($trunc_length <= $letter_count){$string = substr($2,0,$trunc +_length);return $string;} # if the final string is bigger than the beginning spaces + text if ($trunc_length > $space_text_length) {$string = "$1$2" . subs +tr($3,0,$trunc_length - $space_text_length);return $string;} # if the final string is bigger than text, but smaller than begi +nning spaces + text if ($trunc_length > $letter_count && $trunc_length < $space_text +_length){$string = substr($1,0,$trunc_length - $space_text_length) . +$2;return $string;} }

In reply to Re: Truncate string to limited length, throwing away unimportant characters first. by deMize
in thread Truncate string to limited length, throwing away unimportant characters first. by ambrus

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.