[thelist] Perl string help

Bill Moseley moseley at hank.org
Tue Feb 13 12:53:27 CST 2007


On Tue, Feb 13, 2007 at 12:00:35PM +0000, Struan Donald wrote:
> * 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.

By the way, if you are trying to just wrap the text at some length
then you might look at the standard Text::Wrap module.

Plus, if you really want separate strings then maybe:

    $Text::Wrap::coulmns = 40;
    my @lines = split /\n/, wrap('','', $text );


$ cat t.pl
#!/usr/bin/perl
use strict;
use warnings;
use Text::Wrap;
use Data::Dumper;


my $text = '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.';

$Text::Wrap::columns = 40;

my @lines = split /\n/, wrap('','', $text );

print Dumper \@lines;

$ perl t.pl
$VAR1 = [
          '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.'
        ];





-- 
Bill Moseley
moseley at hank.org




More information about the thelist mailing list