in reply to Smart Substrings
If you don't care about matching multiple spaces or about matching tabs, I would rewrite the second and third-to-last lines in the sub as follows (which would essentially make it bbfu's solution):sub shorten { local $_ = shift; my $len = shift; my $pos; return $_ if $len >= length; $len -= 3; $pos = pos while /\w(?=\s)/g && $len >= pos; $pos ||= $len; substr($_, 0, $pos)."..."; } print shorten("misunderestimate", 15), "\n"; print shorten("all work and no play makes MeowChow a dull boy", 15), " +\n"; print shorten("imapirateiamiam", 15), "\n";
This would make a good Golf contest by the way (hint, hint)...$pos = rindex $_, " ", $len; $pos = $len if $pos < 0;
MeowChow s aamecha.s a..a\u$&owag.print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Smart Substrings
by cLive ;-) (Prior) on Apr 03, 2001 at 09:00 UTC | |
by MeowChow (Vicar) on Apr 03, 2001 at 09:21 UTC |