Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uwm.edu!ogicse!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal L. Schwartz) Newsgroups: comp.lang.perl Subject: Re: Comparing numbers and regular expressions Message-ID: <1991Feb2.014512.23396@iwarp.intel.com> Date: 2 Feb 91 01:45:12 GMT References: <5152@s3.ireq.hydro.qc.ca> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal L. Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 81 In-Reply-To: gamin@ireq-robot.hydro.qc.ca (Martin Boyer) In article <5152@s3.ireq.hydro.qc.ca>, gamin@ireq-robot (Martin Boyer) writes: | $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. | | Can anybody enlighten me? Yeah. (I hope.) Look at the regular expression you are building: /^13|1$/ This matches anything that starts with a 13, or anything that ends in a 1, of which 101 is clearly an example. What you want instead is: /^(13|1)$/ And, backtracking that into your code: ################################################## $uid_list = "13|1"; $this_uid = 101; if ($this_uid =~ /^($uid_list)$/o) { print "Matches!\n"; } else { print "Doesn't match.\n"; } ################################################## On which I get "Doesn't match.". I wouldn't do it that way, actually. I'd opt first for an assoc array, as in: ################################################## for (13,1) { $cool{$_}++; } if ($cool{101}) { print "Yes!\n"; } else { print "No!\n"; } ################################################## If the values of array are well behaved, I could go for a bitmap, like: ################################################## $cool = ""; for (13,1) { vec($cool,$_,1) = 1; } if (vec($cool,101,1)) { print "Yes!\n"; } else { print "No!\n"; } ################################################## There's More Than One Way To Do It, sez Larry. print pack("h*",unpack("H*","\244W7G\002\026\346\366G\206V'\002\005V'\306\002\206\0266\266V'\302")) -- /=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: "Intel: putting the 'backward' in 'backward compatible'..."====/