#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    1
    Rep Power
    0

    Question case insensitive string compare in perl


    can anyone tell me how to do a string compare in perl
    which is case insensitive. Something that would make
    cat=CAT
    cat=Cat
    cat=cAt
    cat=CaT
    All of the above statements would be true.
    Thanks in advance. I am confident ther is a perl trick thatl can do what I want
    jodell
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    North Vancouver, BC, Canada
    Posts
    44
    Rep Power
    14

    Hmmmmm..... Regular Expressions


    Try saving code as bob.pl and run on the command line.

    run as :
    perl bob.pl CAT -- where cat|CAT|DOG is going to be $mary

    Code:
    $bob ="cat";
    
    # Assign mary from command line
    
    $mary =$ARGV[0];
    
    # i means case insensitive
    if ($bob =~/$mary/i) {
        print "bob matches mary\n";
    	}
    else {
        print "bob does not match mary\n";
    	}
    Then do a search on the net or look up "Regular Expressions"
    in a perl book.

    =======================================
    Apologies this is a bad example for the use of checking
    if the 2 values are the same.
    Use JonLed's example.
    My fingers where in motion before my brain was in gear.
    ========================================
    Thanks

    Foot in Mouth ver 1.2.5 Onion
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    16
    There are a few problems with that, but I'm just going to offer a totally different alternative.
    Code:
    if(lc($string) eq 'cat') {
       # true
    }
    You can see how this avoids all the problems with a regex and is also faster to execute.

    BTW, lc() forces everything to lower case.
    Jon Coulter
    ledjon@ledjon.com
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    1
    Rep Power
    0

    Red face


    thanks,
    these are both good solutions, now that I know a litle more,
    here is my full problem, (I am going to cross forum boundaries here but stay with me please)
    I have a site that has a mysql database that users can input into and also search from. When a user logs on I keep track of them with cookies to make sure they are only modifying or deleting there own files and not someone else's. Mysql is the database but everything else is tied together with perl. When a user logs on a form sends a command to query the database to match up their userID and password. It also sets a cookie with the userID and password. The mysql table presently is not case sensitive so you can log in even if you type the wrong case. However when perl checks the userID against the cookie if the cases do not match I send back an error message( the program thinks the data file is being accessed by a wrong user)

    Here comes the question! should I make the mysql table binary to make it case sensitive and allow users to have the same userID but with different case. or should I make the perl case insensitive by using a regex or forcing everything to lower case.

    I always get myself in trouble like this and need help.
    any suggestions or insights would be greatly appreciated.

    Lost in CYBERSPACE,
    Johnny O

IMN logo majestic logo threadwatch logo seochat tools logo