http://www.perlmonks.org?node_id=375652


in reply to Re^3: In search of a better way to trim string length
in thread In search of a better way to trim string

This is cool but it suffers from a problem that the first one I wrote did too. Give it:

my $mouse = 'This is a story about mice, "I dint know mice were so sma +rt."'; print trimTo($mouse,30), "\n";

And you can get back things like: This is a story about mice,... The comma (or semi-color, period, quote, etc) is a bit jarring.

This is a recent stab I've ended up using but I would love to see other ideas/answers/hybrids:

sub chop_to_size { my ($text, $length) = @_; return $text if length $text < $length; # make room for ellipsis my $chop = $length - 3; $text =~ s/^\s*(.{$chop})\s*.+$/$1/; $text =~ s/[\s[:punct:]]+$//; $text .= "..."; $text; }

Replies are listed 'Best First'.
Re^5: In search of a better way to trim string length
by dragonchild (Archbishop) on Jul 19, 2004 at 19:37 UTC
    The issue arises from what constitutes a "word". The basic definition is @words = split ' ', $line;, which works in most cases. However, it's arguable that a better definition could be @words = $line =~ /(\b\w+(?:['-]\w+)\b)/;. Of course, you're now depending on the definition of \w, which includes underscore and doesn't include apostrophe or hyphen. *shrugs* YMMV a huge amount. Parsing any natural language is much harder than parsing Perl which, as everyone knows, can't be done in Perl.

    Good luck! I mean it.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re^5: In search of a better way to trim string length
by BrowserUk (Patriarch) on Jul 19, 2004 at 20:12 UTC

    According to a writer's guide grammatically, if a quote is truncated after a commas or a full stop, the comma or fullstop should be left in place with no intervening spacing.

    Blah blah blah,...
    Blah blah blah....

    It's an interesting problem though. I think on it and get back to you:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon