in reply to Mixing the case of a line

You use the \u/\U and \l/\L operators in conjunction with a regex. \u uppercases the next character, \U to all following characters. Likewise with \l, except for lower-case. You can stack them too, like \u\L, which will make the next character upper-case, but all subsequent ones lower-case.
$_ = ""; s/\b(\w+)\b/\u\L$1/g; s/(\w+\s*)$/\U$1/g;
Thus, "tHis is a TEST line oF,teXT" will become "This Is A Test Line Of,TEXT". If you have a lot of debris, like brackets, commas and whatnot, you might have to tweak this a bit to better match your definition of "word". Do you want it to be "Free-Lance" or "Free-lance"?

Replies are listed 'Best First'.
Re: Re: Mixing the case of a line
by danger (Priest) on Nov 16, 2001 at 02:32 UTC

    Just to further highlight some of the things tadman mentions: Strings may have punctuation after the final word and some words may have an apostrophe or hyphen(s). You can tune a regex to grab whatever you consider to be words in a s/// operation and apply the \u and \L escapes as needed in the replacement. Here is one potential solution:

    #!/usr/bin/perl -w use strict; while(<DATA>){ s/\b(\w+(?:['-]\w+)*)\b/\L\u$1/g; s/(\w+(?:['-]\w+)*[\W\s]*)$/\U$1/; print; } __DATA__ tHis IS just one exAMPle, note: some STRINGS haVe apOStrophEs or hyPHens, and perhaps faulty punctuation or trailing elipsis ... doN't SETTLE for over-THE-counter sOLutions! !! including this one.

    I hope this isn't homework, but if it is please do yourself a favor and read up on regexen (perlre) and the various case-functions (uc, lc, ucfirst, and lcfirst), and their equivalent double-quoted string escapes (perlop, in the section "Quote and Quote-like Operators").