Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Parens in a reg exp Message-ID: <9488@jpl-devvax.JPL.NASA.GOV> Date: 13 Sep 90 01:37:52 GMT References: Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: comp Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 55 In article Mike.McManus@FtCollins.NCR.com (Mike McManus) writes: : : I'm reading tokens in from a file and doing compares on the token, sort of : like: : : @toklist = ("VDD", "GND", IN", "OUT", "INOUT"); : : while(<>) { : if( do IS_TERM()) { : ... : } : } : : sub IS_TERM() { : $token = (split)[1]; : if( join( " ", @tklist) !~ /$token/) {return(0);} : foreach $i (@toklist) { : if( $i eq $token) { : .... : return(1); : } : } : return(0); : } : : # Warning! Not the REAL perl code, but a resonable facsimile thereof... : : The problem I run into is when $token is something containg a meta character. : Specifically, things die when $token = "IF(". Anybody got any ideas about a : work around? I tried some things, but am a green enuff novice that I didn't : have much luck. : : NOTE: I use the 'if(join...) return 0' as a quick and dirty check, before : launching into a 'foreach $i (@toklist)', so as to save time. Will it, can : forget about it. This would solve my problem, too. To solve your immediate problem, quote metacharacters by saying $token =~ s/(\W)/\\$1/g; However, any time you're doing a linear search in Perl, you're probably doing it wrong. Learn to think in terms of associative arrays. What you want is something like this: %isterm = ("VDD", 1, "GND", 1, IN", 1, "OUT", 1, "INOUT", 1); while(<>) { ($junk, $token) = split; if( $isterm{$token} ) { ... } } Larry