On Jun 10, 2004, at 9:46 PM, Beau E. Cox wrote:

Hi -

I am trying to come up with a simple, elegant word parsing script, that:

* takes a scalar string, and
* splits it into words separating on white space, commas,
  and a set of delimiters: "" '' // () {} [] ##, and
* returns the array of words.

[snip code]

There's really not a lot wrong with you're version, save that all that use of substr() and pos() makes me think too much to figure out what it's doing. Here's how I would do the same thing:

sub parse_words {
        my $line = shift;

        my @words = ();
        while ( $line =~ s/^\s*(["'\/#])(.*?)\1\s*// or
            $line =~ s/^\s*(\(.*?\)|\{.*?\}|\[.*?\]|<.*?>)\s*// ) {
                push @words, $2 || substr $1, 1, length($1) - 2;
        }
        push @words, split m/[,\s]+/, $line if length $line;
        return @words;
}

I'll leave it to you to decide if that's "elegant" though.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to