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:
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 $_.
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
A simple example about how to use it:# initialize some string variable
my $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
my $str1 = " rose";
my $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";
}
If you want to download the Perl lc script with all the above examples included, please click here: Script download