timtowdi! all purpose-- can be run through strings shorter than 20 characters with no bad effect.
my $foo="asdhflakjsdfhlaksjdfhlaksdjfhalks"; $foo =~ s/(.{20}).{3,}/$1../; print "-"x 20, "\n"; print $foo, "\n"; my $foo="asdhflakjsdfhlaksjdfh"; $foo =~ s/(.{20}).{3,}/$1../; print "-"x 20, "\n"; print $foo,"\n";

friendly golf challenge : given a string, and a length, truncate the string in the middle, using 3 periods (an ellipsis) to indicate the truncation. The truncation must be even, that is, the same number of characters must appear on each side of the ellipsis, plus or minus 1. The length given should be the final length of the string. Length will always be less than the string.
example  f("Just another perl hacker",11) = "just...cker"
example  f("Just another perl hacker",12) = "just ...cker" or "just...acker", depending on your preference. update 71 chars :
use integer; my $foo="Just another perl hacker"; print f2("Just another perl hacker",12); sub f2 { ($_,$l)=@_;$s=$l-3;$n=$s/2;$m=$n+($s&1);s/(.{$m}).*(.{$n})/$1...$2/;$_ }

In reply to Re: Cutting down a string (boo) by boo_radley
in thread Cutting down a string by Anonymous Monk

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.