in reply to Splitting SPICE lines on 'word' boundries closest to a certain length

Looking at your while loop, you should notice that when $' is an empty string, and you do this:
$rest = "+ " . $';
the condition for exiting the while loop -- $rest eq '' -- will never be met. The easiest fix (involving the smallest change to the existing code) would be:
$rest = ( $' ) ? "+ $'" : '';
or something equivalent. Personally, though, I tend to prefer (r)index and substr for this sort of problem -- something like this (using index, because you seem to be willing to accept longer strings when looking for a place to break):
while ( length($rest) > 25 ) { my $break = index( $rest, ' ', 25 ); last if ( $break < 0 ); # no more spaces to break on push @text, substr( $rest, 0, $break ); $rest = '+ ' . substr( $rest, $break ); } push @text, $rest if ( length( $rest ));

Replies are listed 'Best First'.
Re: Re: Splitting SPICE lines on 'word' boundries closest to a certain length
by gcmandrake (Novice) on Mar 23, 2004 at 17:19 UTC
    Thanks to all you monks for your elegant (and thought provoking) solutions. My customer was delighted with the code, but now wants more features. And so it goes... Thanks again. gcmandrake