[thelist] Perl string help

Amy Johnson amy at rocky-hills.com
Tue Feb 13 23:37:14 CST 2007


Thank you!  This helped tremendously!
Amy


> * at 12/02 22:29 -0700 Amy Johnson said:
> > Can anyone help me with a Perl string function?  I need to take the 
> > text from a textarea and split the string at the last blank space 
> > before 250 characters.  So if I have a 1000 character 
> string, it would 
> > be cut into about 4 or 5 strings at spaces between words.
> >  
> > Any Perl gurus out there?
> 
> I am not a Perl guru but...
> 
> assuming $text contains the text you want to split up then 
> the following should work. At the end you'll have an array 
> (@pieces) which contains all the pieces.
> 
> my $piece_length = 250;
> 
> my @pieces;
> my $start = 0;
> 
> while ( ( length($text) - $start ) > $piece_length ) {
>     my $split = rindex( substr( $text, $start, $piece_length ), ' ' );
>     push @pieces, substr($text, $start, $split);
>     $start += $split;
> }
> 
> push @pieces, substr($text, $start);
> 
> cheers
> 
> Struan
> 
> 





More information about the thelist mailing list