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

Ok...

like, I am really new to perl and what not, so please don't make fun of me or I will cry and be forced to leave my home and forge through trash cans and what not...

ok... so here we go. If I write an If statement and want to check to see if um... let's say $word and $word1 are equal, $word1 being from a database, how could I check to see whether they are equal, regardless of the case of the letters.

For example.

I want Cheese to be equal to cheese

how would I do that?

  • Comment on Turning off case sensitivity when using conditions

Replies are listed 'Best First'.
Re: Turning off case sensitivity when using conditions
by Kanji (Parson) on Apr 24, 2001 at 06:24 UTC

    Force both halves of the comparison to be the same case using lc() or uc(), perhaps even \L or \U depending on your needs.

    if ( lc($word) eq lc($word1) ) { ... }

        --k.


Re: Turning off case sensitivity when using conditions
by rchiav (Deacon) on Apr 24, 2001 at 06:20 UTC
    What you're probably going to want to do is compare them as if they were lower case. For this, look at the lc function. And no.. we won't make fun of you. Well, not until we get to know you better :)

    Hope this helps..
    Rich

Re: Turning off case sensitivity when using conditions
by tune (Curate) on Apr 24, 2001 at 06:37 UTC
    For the sake of TMTOWTDI:
    if ($var =~ /^cheese$/i) { # got it }
    That is regexp power man, but it is not the best solution. And I did not claim it is the fastest. (now comes the benchmark people:)

    -- tune

      Try that with $var set at "cheese\n".

      I think you want \z rather than $ there...

Re: Turning off case sensitivity when using conditions
by diarmuid (Beadle) on Apr 24, 2001 at 17:00 UTC
    if ($word1 =~ /\b$word2\b/i) if (lc($word1) eq lc($word2)) if ($word1 =~ /^$word2$/i)

    I'm sure there are plenty more