in reply to next if attempt

What is your exact issue. I find nothing wrong with the code (besides being needlessly verbose and containing an unneeded /g, see below). The regexp should work as far as I can see. Indeed, it works find on this test program:
use strict; my $wordOne = 'longwordOne'; my $wordTwo = 'anotherLongWord'; my $wordThree = 'thirdLongHere'; while (<DATA>) { next if ($_ =~ /(?:$wordOne|$wordTwo|$wordThree)/gi); print ; } exit; __DATA__ blah blah longwordOne blah blow blow blee anotherLongWord glgl thirdLongHere fddsf ds sdfs fs fsd fggh hg longwordOne sdasd

I get:
blow blow fddsf ds sdfs fs fsd sdasd
and the next can be written more succinctly as:
next if /$wordOne|$wordTwo|$wordThree/i;


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: Re: next if attempt
by LTjake (Prior) on Jul 16, 2003 at 13:18 UTC

    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

      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