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

To make it a bit easier to update, one might write it as:

my @stopwords = qw( longwordOne anotherLongWord thirdLongHere ); my $stopmatch = join( '|', @stopwords ); while( <DATA> ) { next if /$stopmatch/i; print; }

Thus you only have to update the @stopwords array to add another stop-word.

--
"To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Replies are listed 'Best First'.
Re^3: next if attempt
by particle (Vicar) on Jul 16, 2003 at 13:40 UTC

    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*

      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.

      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*