in reply to Reverse grep?

I am trying to do something like the following but can't get it to work:
open FILE, "regex" or die "Couldn't open: $!"; while (<FILE>) { if ( $ENV{"REDIRECT_URL"} +~ /$_/ ) { print $_; last; } } close FILE;

Where the file "regex" would contain all of the patterns I want to test against the URL.

Replies are listed 'Best First'.
Re: Re: Reverse grep?
by dws (Chancellor) on Mar 02, 2002 at 00:47 UTC
    You need to add this:
    while (<FILE>) { + chomp; if ( $ENV{"REDIRECT_URL"} +~ /$_/ ) { print $_; last; }
    Otherwise, you have a trailing newline at the end of your pattern.

Re: Re: Reverse grep?
by coolmichael (Deacon) on Mar 02, 2002 at 05:23 UTC
    I think +~ might be a typo. Don't you want this instead?
    if ( $ENV{"REDIRECT_URL"} =~ /$_/ ) { print $_; last; } ^

    Please correct me if I'm wrong.

      Considering that one must hold down the shift key for the tilde character, coupled with the fact that the plus sign character shares the same key as the equal sign character ... yes ;)

      But, have you played with the results yet?

      [prompt]$ perl -le 'print /RED/' [prompt]$ perl -le 'print ~/RED/' 4294967295 [prompt]$ perl -le 'print hex("F"x8)' 4294967295 [prompt]$ perl -le '$_ = "REDIRECT"; print /RED/' 1 [prompt]$ perl -le '$_ = "REDIRECT"; print ~/RED/' 4294967294 [prompt]$ perl -le '$_ = "REDIRECT"; print 5+~/RED/' 4294967299
      Gotta love Perl's flexibility! :D

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)