allison has asked for the wisdom of the Perl Monks concerning the following question:

form a given file after the AU: tag the first letter of each word must be capitalize? sample AU: jane smith output AU: Jane Smith
my $filename = "filename"; open my $file, '<', $filename; @fileinput=<$file>; close($file); foreach $line(@fileinput) { my $Cword=($line); if($Cword=~s/(^AU:\s[a-z])/(^AU:\s[A-Z])/) { print $Cword; } }
the output for that code is (^AU:sA-Z)ane smith, why is that? thanks

if($test=~s/AU:\s\b(\w+)\s\b(\w+)|AU:\s\b(w+)/AU: \u$1 \u$2/g) I tried this code to capital the first letter of each word the code is only valid with AU: Jane Smith but when in a file has a name of 'mr. carlos smith' it does not capitalize the each word. Is there any particular regex that i can use without using the Use Lingua::EN::NameCase.

Replies are listed 'Best First'.
Re: Make the first letter of each word capitalize
by Eliya (Vicar) on Mar 20, 2011 at 10:37 UTC

    Because the replacement part of a substitution is not a regular expression.  Try

    s/^AU:\s([a-z])/AU: \u$1/

    (doing the same for the second name is left as an exercise...)

    See also ucfirst.

Re: Make the first letter of each word capitalize
by wind (Priest) on Mar 20, 2011 at 15:42 UTC

    Use Lingua::EN::NameCase.

    You're not dealing with words, it appears you're dealing with names. Therefore, use the below example in order to take advantage of all the special cases that ucfirst lc will miss.

    use Lingua::EN::NameCase qw( nc ); use strict; while (<DATA>) { s/(?<=AU: )(.*)/nc $1/e; print; } __DATA__ AU: jane smith AU: JOHN DOE AU: keith miller AU: PHILLIP LEIGH-WILLIAMS AU: PAUL MCCARTNEY AU: DOUGIE O'CALLAGHAN AU: MARIE ST. JOHN AU: henry viii
    The above code runs everything after the AU tag through nc and outputs the result, like so:
    AU: Jane Smith AU: John Doe AU: Keith Miller AU: Phillip Leigh-Williams AU: Paul McCartney AU: Dougie O'Callaghan AU: Marie St. John AU: Henry VIII
    - Miller
Re: Make the first letter of each word capitalize
by Khen1950fx (Canon) on Mar 20, 2011 at 13:11 UTC
    Using Text::Capitalize:
    #!/usr/bin/perl use strict; use warnings; use Text::Capitalize; my $filename = '/root/Desktop/name_list'; open my $fh, '<', $filename; my @fileinput = <$fh>; foreach my $line(@fileinput) { print capitalize("AU: $line", "\n", PRESERVE_WHITESPACE => 1, PRESERVE_ALLCAPS => 1); } close $fh;
    The name_list that I used:
    jane smith john doe jesse james james madison bill clinton bill hickok larry wall, minnesota fats