Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change first character on a line with upper case perl

Tags:

This is a simple question I guess, but I was trying to change just the first lower case letter of a line from a .txt file to an upper case, using the following

$_ =~ s/^[a-z]/\U/; 

What happens, when I execute it, is that instead of changing the lower case to upper case the lower case at the beginning of the line is substituted with the most significant bit on the line. For example, the line nAkld987aBALPaapofikU88 instead of being substituted with NAkld987 becomes Akld987...

like image 308
TheBlackCorsair Avatar asked Nov 21 '12 16:11

TheBlackCorsair


People also ask

How do you replace the first character in a string with uppercase?

You can use str. title() to get a title cased version of the string. This converts the first character of each word in the string to uppercase and the remaining characters to lowercase.

How do I make the first letter capital in Perl?

Perl | uc() Function uc() function in Perl returns the string passed to it after converting it into uppercase. It is similar to ucfirst() function which returns only first character in uppercase.

What is LC in Perl?

The Perl lc() function takes a string, makes the entire thing lowercase and then returns the new string. For example: #!/usr/bin/perl.


1 Answers

You could/should use ucfirst. I say should as it's much more obvious to a maintainer that your intent is to uppercase the first letter of the string. I love a regex, but in this case I feel it's not the correct approach.

my $str = "test"; print ucfirst($str); 
like image 88
RC. Avatar answered Sep 27 '22 16:09

RC.