Home
Perl How To
Perl Books
Perl Basics
Perl Editors
Perl Modules
Perl Database
Net Programming
Sitemap
About Me
Contact Me
Blog
 

Perl lc Function



NEW!!!

Check my new resource:    Perl How To Tutorial eBooks



Perl lc function (lc stands for lowercase) accepts as parameter an expression and returns a lowercase version of that expression. In this short tutorial we’ll give you some useful examples that show you how to use this function in your script applications. There is of course the function uc (uppercase) which returns an uppercase value of a string expression. Click the uc link to see what uc function is about.

We have two syntax forms for this function:

lc EXPR 
lc

Perl lc function doesn’t change the content of a EXPR string variable, it merely returns a lowercase copy of the string. If the EXPR is omitted, the function is called against the special variable $_.

A simple example about how to use it:

# initialize some string variable
$str = "What is Perl Language for";
lc($str);
print $str, "\n";
# displays: What is Perl Language for
$str = lc($str);
print $str, "\n";
# displays: what is perl language for

In the third line of this snippet code, we converted $str to lowercase, but without an assignment our conversion was lost. The next lines of the code show you how to do it.

Perl lc function is very useful when you need to perform some string comparisons against some string variables. Bear in mind that all string comparisons are case sensitive and the following strings "Perl" and "perl" are not the same. As a parenthesis, don’t ever use numerical operators in string comparisons, because it’s for sure the result will not be the one you would expect.

Now let’s get back to our comparisons. If you want to compare two string variables case insensitive using lc function, you may have at least two choices:

  • Convert both strings in lowercase using an assignment operator and than make the comparisons
  • Compare the strings by calling the lc function for each of them, without changing their values
See the next short example about how to do this:

#comparison using the assignment operator
$str1 = " rose";
$str2 = " ROSE";
$str1 = lc($str1);
$str2 = lc($str2);
if($str1 eq $str2)
{
  print "The two strings are equal\n";
}

#comparison without altering the string values

$str1 = " rose";
$str2 = " ROSE";
if(lc($str1) eq lc($str2))
{
  print "The two strings are equal\n";
}





NEW!!!

Do you want more information about the basic Perl topics?

Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :


Perl How To Tutorial eBooks


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Array Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        oct
        ord
        pack
        pop
        push
        reverse
        rindex
        shift
        split
        substr
        uc
        undef
        unshift

return from Perl lc Function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!


footer for perl lc page