Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to nullify case sensitivity

by iphone (Beadle)
on Nov 24, 2010 at 07:37 UTC ( [id://873373]=perlquestion: print w/replies, xml ) Need Help??

iphone has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the below if condition,how do I make sure to nullify the caps lock difference in the condtion?

if ($b ne $a) { }

Replies are listed 'Best First'.
Re: how to nullify case sensitivity
by GrandFather (Saint) on Nov 24, 2010 at 08:03 UTC

    Use lc or uc to normalise the strings you want to compare:

    if (lc $var1 ne lc $var2) { ... }

    Note by the way that $a and $b are special variables used by sort and should generally not be used for other purposes - even in example code.

    True laziness is hard work
Re: how to nullify caps lock
by Anonymous Monk on Nov 24, 2010 at 07:50 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to nullify case sensitivity
by kcott (Archbishop) on Nov 24, 2010 at 08:05 UTC

    Are you aware that $a and $b are special variables (see perlvar) used by sort. Unless you are using them in that context, you would be well advised to pick different variable names.

    The condition you're after can be written as:

    if (lc($var1) ne lc($var2)) { ... }

    -- Ken

Re: how to nullify case sensitivity
by CountZero (Bishop) on Nov 24, 2010 at 09:59 UTC
    There is more than one way to do it:
    use Modern::Perl; while (my $first = <DATA>) { my $second = <DATA>; chomp $first; chomp $second; say "Is >$first< equal to >$second<? ", ($first =~ m/^$\Qsecond\E$ +/i)?'Yes':'No'; } __DATA__ test TEST testing testing TESTED TESTED TeStS tEsTs a Test another Test
    Output:
    Is >test< equal to >TEST<? Yes Is >testing< equal to >testing<? Yes Is >TESTED< equal to >TESTED<? Yes Is >TeStS< equal to >tEsTs<? Yes Is >a Test< equal to >another Test<? No
    Update: added the quotemeta tags \Q \E.</inq>

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Indeed! Thank you for reminding me.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: how to nullify case sensitivity
by ikegami (Patriarch) on Nov 24, 2010 at 16:43 UTC

    If you wanted to use Unicode's definition of case insensitivity — they are the experts in this area — you should convert the strings to fold case (not lower case or upper case) and then compare them. The simplest way of doing that is:

    $s1 =~ /^\Q$s2\E\z/i

    Like the lc and uc methods, this assumes the string has previously been normalised. All together,

    use Unicode::Normalize qw( NFC ); $s1 = NFC($s1); $s2 = NFC($s2); $s1 =~ /^\Q$s2\E\z/i

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://873373]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found