in reply to 30 Spaces- 1 question

My recommendation:

my @w = split /\s+/,$text,31; pop @w; my $words30= join " ", @w;

dchetlin's regex I like too: (I modified it a little)

$text =~ m/^((?:\S+\s+){1,30})/; my $words30 = $1;

Tho with that all the usual warnings about $1 being set to a prior match may need dealing with.

If ABSOLUTELY sure there are no double spaces or other nastiness, you could use index() in a for loop too. But it's ugly so someone else can do that...

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
RE: Re: 30 Spaces- 1 question
by dchetlin (Friar) on Oct 09, 2000 at 07:01 UTC

    I appreciate your liking my REx, but be careful; that \s* is a `*' for a reason. Try yours out:

    [~] $ perl -wnle'/^((?:\S+\s+){1,30})/;print $1' one two three four five six seven one two three four five six

    versus mine:

    [~] $ perl -wnle'/^((?:\S+\s*){1,30})/;print $1' one two three four five six seven one two three four five six seven

    Also, your split solution has a minor problem:

    [~] $ perl -wnle'@w=split/\s+/,$_,31;pop@w;print join " ",@w' one two three four five one two three four

    (Granted, the problem specs weren't that great, but it seems reasonable to assume that if we have a line with less than 30 words, we don't want to throw away the last.)

    -dlc

      Actually, I had rather taken it as gospel that the string was more than 30 chunks. =) good spot on that.

      I still hate throwing the regex engine at this problem tho. Maybe I'll Benchmark em all and post that. Make me feel better for being a goof.

      --
      $you = new YOU;
      honk() if $you->love(perl)

        I wouldn't have posted if it was just the problem with your split solution, because it's highly minor and can be easily fixed. I was more concerned with the REx one because it always leaves off the last chunk in the string unless the string ends in whitespace.

        I would definitely be interested in seeing a Benchmark of this. Keep in mind, though, that split uses the REx engine just as much as m/// or s///.

        -dlc