Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Parens in a reg exp Message-ID: <1990Sep13.013737.10897@iwarp.intel.com> Date: 13 Sep 90 01:37:37 GMT References: Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: comp Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 55 In-Reply-To: Mike.McManus@FtCollins.NCR.com (Mike McManus) In article , Mike.McManus@FtCollins (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); | } Shoot. You're doin' it the hard way. Give the task to Perl in the form of a regex that matches *everything* you wanna lookfor in one fell swoop... ################################################## snip here @toklist = ("VDD", "GND", "IN", "OUT", "INOUT"); grep(s/\W/\\\&/g,@toklist); # de magic-ize toklist eval 'sub is_token { $_[0] =~ /(' . join('|',@toklist) . ')/; }'; # add ^ and $ here if you want anchored matches (I think you do) #testing one two three... for ('aaa','bbb','VDD','ccc','ddd') { print "$_ ", &is_token($_) ? "IS" : "isn't", " a token\n"; } ################################################## snip There. OK? print "Just another token Perl hacker," # :-) -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/