 |
| |
Perl lcfirst Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
No much to say about Perl lcfirst function. It takes a string, converts its first character in lowercase and than returns the new string. There are not many cases where you can use it and it was probably added as a counterpart of the ucfirst function. However, I’ll do my best to show you how to use it in applications.Like for the other simple string functions, the syntax form is as follows: It returns the value of EXPR with its first character in lowercase. If the argument is a string variable, this function returns the variable with its first character lowercased, but it doesn’t alter the content of the variable. In the second syntax form, EXPR is omitted, so lcfirst function uses the special variable $_. As with other functions, you can use or omit the parentheses, as you like. If the first letter of the string expression is lowercase, the function will do nothing.Now consider the following example to see how Perl lcfirst works: # initialize a string variable
$str = "An example about lcfirst function";
$newStr = lcfirst $str;
print "$newStr,\n";
# prints: an example about lcfirst function
Next, we are going to examine a more "sophisticated" example. Let’s suppose we have a title where all the words begin with an uppercase and we want to convert the first letter of each word in lowercase, except the first word of the title. Take a moment to examine the next snippet code:# store the title in a scalar string variable
$title = "A Short Story About Myself";
# split the string and place it into an array
@title = split / /, $title;
# now let’s iterate through the array
$flag = 0;
foreach (@title) {
if($flag == 0)
{
$flag = 1;
next;
}
$_ = lcfirst;
}
# convert the array to a space-delimited string
$title = join(" ", @title);
print "$title\n";
Some comments here:- we store the title in $title variable
- we split the string into an array using the space delimiter
- in the foreach loop:
- we use $flag variable to bypass the first word of the string in order not to change it
- we call lcfirst against $_
- we store the new word value in $_
- we use the join function to convert the array back into the string using the space-delimiter
- finally, we print the string
The output for this example is the following:A short story about myself
Don’t take this example too seriously, perhaps 2-3 lines of code are enough to do this by using regular expressions, but I wanted to show you how to use Perl lcfirst function together with some other functions.That’s enough for now.
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 lcfirst Function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!


|
|