Looking through the FAQs linked to a previously question on “how to capitalise the first letter”, I found a lot of interesting solutions to the problem that I would like to try, but I have a more pressing issue to deal with, before getting there.
My problem is that I cannot strip the white space from the beginning of a name on each line. The names comprise first name and surname. The surnames are capitalised. I have tried to use the solutions found in the FAQ, edited by Brian D Foy, but whatever I try, I get Use of uninitialised value $_ in substitution (s///) at ...
My code is this:
open DATA, “<“, “name.txt”;
my @file = <DATA>;
for each my $name (@file) {
chomp $name;
my @arr;
# find ‘Mr.’ and ‘Ms.’, then ignore them
@arr = split(/M.\.\s/g, $name);
# remove leading white space from first names
@arr = s/^\s+//g;
print @arr,”\n”;
}
That is part of an exercise. My solution bears little resemblance to the author’s own. Here, I am actually supposed to capitalise the first letter of each name. I cannot get passed the present issue to complete the exercise. Though the code is quite long as a solution to the stated aim, I wanted to utilise the methods described in the study aid, before attempting to find more succinct ways of achieving the same outcome. For now, I would greatly appreciate clarity as to why I am getting the error message and why I cannot remove white space. Thank you.
You can remove the white spaces from the names in the split regex.
So your code will become:
open DATA, "<", "name.txt";
my @file = <DATA>;
foreach my $name (@file) {
chomp $name;
# find ‘Mr.’ and ‘Ms.’, then ignore them
my @arr = split(/M.\.\s+/g, $name);
print @arr,"\n";
}