Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Comparing numbers and regular expressions Message-ID: <11278@jpl-devvax.JPL.NASA.GOV> Date: 2 Feb 91 01:11:57 GMT References: <5152@s3.ireq.hydro.qc.ca> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 41 In article <5152@s3.ireq.hydro.qc.ca> gamin@ireq-robot.hydro.qc.ca (Martin Boyer) writes: : : I'm trying to compare a numeric user id against a list of numbers. : Here's my attempt; it doesn't work, possibly because the regular : expression search doesn't treat the numbers as strings: : : $uid_list = "13|1"; : : $this_uid = 101; : : if ($this_uid =~ /^$uid_list$/o) { : print "Matches!\n"; : } else { : print "Doesn't match.\n"; : } : : This section prints Matches!, but that's not what I want; what I want : is for the test to succeed only for values of 13 or 1. Well, look at the pattern you're actually searching for: /^13|1$/ Note that ^ and $ don't automatically distribute over alternatives. /1$/ matches 101. You want /^(13|1)$/. So say /^($uid_list)$/ and it should work. Note that this is essentially a linear search. If you're doing lots of comparisons against a longish list, it'll be much faster to load up an array to check with: for (split(/\|/, $uid_list)) { # or equivalent $valid_uid[$_] = 1; } ... if ($valid_uid[$this_uid]) { ... If your uids get too large to use a normal array, use an associative array instead. A vec() would also work, and save a lot of space over a normal array: if (vec($valid_uid, $this_uid, 1)) { ... Larry