Thread: String Compare

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Question String Compare


    I have a string
    $string1 = "4PL400".

    Now i read a string from <STDIN>.....

    now i can enter both 4pl400 and 4PL400.......the question is how do i compare the input to string1.

    In clear words, the input is case insensitive....but the comparison has to be done..

    Please help and advise

    Thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Use the "lc" operator to change both strings to the same case, then compare with "eq".

    Comments on this post

    • Laurent_R agrees
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0
    Thanks Omegazero
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by csvraju1985
    I have a string
    $string1 = "4PL400".

    Now i read a string from <STDIN>.....

    now i can enter both 4pl400 and 4PL400.......the question is how do i compare the input to string1.

    In clear words, the input is case insensitive....but the comparison has to be done..

    Please help and advise

    Thanks in advance
    You do do a case insensitive regular expression match.

    Code:
    $string1 = "4PL400";
    
    $string2 = "4pl400";
    
    if ( $string2 =~ /^$string1$/i )
    {
      print "Matched!\n";
    }
    else {
      print "No match :(\n";
    }

IMN logo majestic logo threadwatch logo seochat tools logo