Perl lcfirst Function
No much to say about the 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 as 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 lowercase, 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
my $str = "An example about lcfirst function";
my $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 code snippet: # store the title in a scalar string variable
my $title = "A Short Story About Myself";
# split the string and place it into an array
my @title = split / /, $title;
# now let’s iterate through the array
my $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 the Perl lcfirst function together with some other functions. If you want to download the Perl lcfirst script with all the above examples included, please click here: Script download That’s enough for now.
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 lcfirst (more) 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 lcfirst Function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!
|