Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pattern matching

    Hi

    I have a string that I want to check for the presence of another string. The only problem is, both strings are stored in variables. How can I do this? I'm assuming I can't just say

    if ($string1 =~ /$string2/)

    because this will search for the actual string "$string2".

    Thanks in advance.

  2. #2
    New Coder
    Join Date
    Aug 2002
    Location
    Moscow, RU
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello,

    well if the string you would like to check for presence of another string located in $string1 variable and checking stuff located in $string2 variable - your regular expression is correct.. if you want to search without capitalization you have to add /i modifier to the end of your regular expression

    like this: if ($string1 =~ /$string2/i)

    hope this will help you..

  3. #3
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Same problem

    I have the same problem as J-dogg,
    when I type $string2 the value of the $string2 (in my case $string2 = 27) is not taken, instead it tries to search for "$string2" (not 27) in the $string1 string.

    Can you please help me....

    Thank you,

  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,206
    Thanks
    10
    Thanked 1,166 Times in 1,157 Posts
    When using compares with strings,
    also make sure you use "eq" and "ne" (equal and not-equal)
    instead of ==

    for strings:

    if($string1 eq $string2){ print "they are equal.";}
    if($string1 ne "orange"){ print "they are NOT equal.";}

    for integers:

    if($xpos == $ypos){ print "the two positions are equal.";}

    I don't know if this helps with your question,
    but just something to keep in mind.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •