in reply to Re: Re: next if attempt
in thread next if attempt

and to make it more accurate, one might write it as (untested:)

my @stopwords= qw/ car pet carpet /; my $stopmatch= join '|' => map { '\b' . $_[1] . '\b' } ## match full words reverse sort { $a->[0] <=> $b->[0] } ## longest first map { [length $_, $_] } @stopwords;

Thus longer words will match first, and only full words will be matched.

~Particle *accelerates*

Replies are listed 'Best First'.
Re^4: next if attempt
by Aristotle (Chancellor) on Jul 16, 2003 at 15:19 UTC
    length is an extremely cheap operation, so an ST is superfluous clutter here. The full-words-only effect does not require an extra map either - just stick the assertions around the final regex.
    my $stopmatch = join '|', sort { length $b <=> length $a } qw(car pet carpet); $stopmatch = qr/\b(?:$stopmatch)\b/;
    Note that sorting on length is only correct as long as you don't have regex quantifier expressions in the "words" - if you do, all bets are off. At any rate, if you're going to this effort, you should probably be using Regex::PreSuf.

    Makeshifts last the longest.

Re: Re^3: next if attempt
by hardburn (Abbot) on Jul 16, 2003 at 14:03 UTC

    That reverse sort isn't necessary (unless I'm missing something). Instead, sort it in the reverse order in the first place:

    my @stopwords= qw/ car pet carpet /; my $stopmatch= join '|' => map { '\b' . $_[1] . '\b' } sort { $b->[0] <=> $a->[0] } ## No reverse here map { [length $_, $_] } @stopwords;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      That reverse sort isn't necessary

      oh yeah, silly me.

      ~Particle *accelerates*