Perl lc Function
The 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: The 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
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
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 Regular Expressions and Pattern Matching List Functions Array Functions Hash Functions Miscellaneous Functions Functions in alphabetical order chomp chop chr crypt defined delete each exists grep hex index join keys lc (more) lcfirst length map oct ord pack pop push q qq qw reverse rindex scalar shift sort splice split sprintf substr tr uc ucfirst undef unpack unshift values
return from Perl lc Function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!
|