in reply to Re: 30 Spaces- 1 question
in thread 30 Spaces- 1 question

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

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

    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