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


in reply to Re: substr question
in thread substr question

Probably better as:

my $s2; for ( split /(\s+)/, $s ) { last if length( $s2 . $_ ) > 100; $s2 .= $_; } print $s2;

Replies are listed 'Best First'.
Re^3: substr question
by toolic (Bishop) on Jun 18, 2010 at 18:55 UTC
    In fact, your code is worse for 2 reasons:
    • The output string $s2 no longer has any whitespace between words: Hello,Iamaperl/mysql...
    • Generates a warning: Use of uninitialized value $s2...