Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!asuvax!cs.utexas.edu!uunet!ksr!bill From: bill@ksr.com (Bill Mann) Newsgroups: comp.lang.perl Subject: All-uppercase text to mixed case Message-ID: <598@ksr.UUCP> Date: 19 Jan 90 16:01:18 GMT Sender: bill@ksr.UUCP Organization: Kendall Square Research Corp. Lines: 53 A few weeks ago Carl Hommel posted a request for a perl script to convert all-uppercase messages to mixed case. I editted his message slightly, *converted it to uppercase*, and ran it through the perl script below. Here's the result: A user asked for help in converting files from uppercase to lowercase, and back again. Of course, there are simple one-liners using tr that will do that. However, i foolishly said that it would be easy to write a program to convert an all uppercase message, which often are sent by our novice users, to a more normal format. The algorithm i thought of is: o uppercase any alpha two spaces after a "." o uppercase any alpha at the beginning of a line, when there was a "." at the end of the previous line. I could brute force this, but that would be work, not fun. I have twiddled with the following features of perl, but got nothing to work: o setting $/ = "\177" and reading in the whole file at once o using if (/\. (.)/) { And $1. I really want an elegant regexp, combined with the tr command. Any ideas? Carl hommel carlton@apollo.hp.com Here's my perl script: #!/usr/bin/perl # This program copies its input to STDOUT, converting uppercase characters # to lowercase according to a simple rule. # RULE: a sentence begins with an alphanumeric and ends at the end of # the input file or at the first terminator (period, question mark, or # exclamation point) which is followed by white space; all but the # first character of each sentence is converted to lower case. $/ = "\177"; # do not split the input into lines $_ = <>; # read the entire input s/(\w)(([^.?!]+|[.?!]+\S)+)/($a=$2)=~tr|A-Z|a-z|,$1.$a/eg; print; # print the results Notes: . The ($a=$2) is necessary, as modifying $2 directly does not work. . The description of $* in the perl manual seems to indicate it should be set to 1, but the script works either way for this example. Bill Mann bill@ksr.com (ksr!bill@uunet.uu.net)