
Capitalize Words in a String
Quote:
> >I finally did a (crude) subroutine that met my needs, but I spent half
> >the day trying to figure out how to capitalize all words in a string
> >using substitutions (i.e. $somestring =~ s/blah/someotherblah/).
> e.g. $string = ~ s/a-z/A-Z/g ..i believe this will substitute all lowercase
> letters to uppercase. the tilde _is_ needed, and the g simply says try to make
> as many global changes as possible.
only if you want to change the substring "a-z" to "A-Z". it's always nice
to try these things before posting them (who said "PERL is an empirical
science"?)
how about:
*****************************
#!/usr/bin/perl
$_ = "this is a string\n";
s/\b(\w)/\U\1/g;
print;
*****************************
with output:
This Is A String
if you just want to capitalized everything indiscriminately, look into the
transliteration operater, tr///;
*****************************
#!/usr/bin/perl
$_ = "this is a string\n";
tr/a-z/A-Z/;
print;
*****************************
with output
THIS IS A STRING