in reply to Checking for occurrence in comma separated string

Geez, your right. I knew something was wrong with that.... Thanks

Now that that is working, how can I resolve the false positives? A small change to the code, shows that false positives are also counting as 'True':
$thestring = "15,130,213"; $thecheck = "13"; if ($thestring =~ /$thecheck/) { print "$thecheck is in $thestring"; } else { print "$thecheck is not in $thestring"; }

What would need to be changed here? The value in $thecheck should only be true if it is in $thestring, without variants.

Replies are listed 'Best First'.
Re^2: Checking for occurrence in comma separated string
by mulander (Monk) on Oct 25, 2005 at 11:25 UTC
    You need to be sure that the whole number is either surrounded by , or the beggining or end of line:
    $thestring = "15,130,213"; if ($thestring =~ /(^13,)|(,13,)|(,13$)/) { print "13 is in $thestring +"; } else { print "13 is not in $thestring"; }
    This is not a very good regexp but it's only an example. I suggest to read more about regular expressions, then start asking question about the difficult parts not the standards covered in almost every regex tutorial.
Re^2: Checking for occurrence in comma separated string
by jdporter (Paladin) on Oct 25, 2005 at 12:37 UTC
    If I knew I was specifically searching for a number in a comma-separated list of numbers, I'd incorporate word boundaries in the search pattern:
    if ( $thestring =~ /\b$thecheck\b/ ) ...
    We're building the house of the future together.
      If I knew I was specifically searching for a number in a comma-separated list of numbers, I'd incorporate word boundaries in the search pattern:

      Which would work right up until you got a number like 1.234e-05... at which point it would die horribly or, worse, quietly go on "working". :-)

      -sauoq
      "My two cents aren't worth a dime.";
      

        Of course. If I had to allow for numbers, and not just natural numbers, I'd DTRT: split on comma, and compare with eq.

        (Update) Whoops! Of course I meant <=>. Naturally. :-)
        sauoq's comments about dealing with whitespace are worthwhile; but the OP's sample data clearly had no whitespace to deal with.

        We're building the house of the future together.