in reply to Rounding to the nearest word

How about this:
#!/usr/local/bin/perl -w use strict; my $string = <<END; Lorem Ipsum is simply dummy text of the printing and typesetting indus +try. Lorem Ipsum has been the industry's standard dummy text ever since the + 1500s, when an unknown printer took a galley of type and scrambled it to make + a type specimen book. It has survived not only five centuries, but also the l +eap into electronic typesetting, remaining essentially unchanged. It was popula +rised in the 1960s with the release of Letraset sheets containing Lorem Ipsum p +assages , and more recently with desktop publishing software like Aldus PageMa +ker including versions of Lorem Ipsum. END my $trunk = substr($string,0,50); # cut to 50 chars $trunk =~ s/\S+$//; # remove ending non-whitespace $trunk =~ s/\s+$//; # remove ending whitespace print "'",$trunk,"'\n";

It guarantees a maximum of 50 chars, but it cuts off the last word if it ends on the 50th char.

Replies are listed 'Best First'.
Re^2: Rounding to the nearest word
by Jasper (Chaplain) on Dec 23, 2004 at 10:00 UTC
    It guarantees a maximum of 50 chars, but it cuts off the last word if it ends on the 50th char.

    substring to 51 characters should solve that, shouldn't it?