[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Change "WORD" to "Word"



Hi all!

>I'm trying to change words that are in all upercase to upper and lower.
>And i can'rt seem to come up with a logical way to achieve this.
>
>Here's my problem... I'm writing a script for our sports section to
>process the NBA boxscores for publication.
>
>Far too often AP sends words in the file in all uppercase and we change
>them. For example: L.A. LAKERS, PHILADELPHIA, SAN ANTONIO. Iturned
>everythig to lowercase with "tr" But now I can't seem to come up with a
>search that will only change the first letter of a word.
>
>Any ideas?

Basically this is a Perl question, and being a Perl newbie, here's my try.
Please feel free to comment, as I'm trying to learn as much as possible.

#!perl
use strict;

my $Test_string = "Some teams like L.A. LAKERS, PHILADELPHIA, and SAN
ANTONIO for the purpose of this exercise.\n";

# Split the sentence into individual words.
my @Atoms = split " ", $Test_string;

my @String;
my $Buffer;

foreach (@Atoms)
{
	# This checks for words that begin with a cap, followed by
	# one or more caps, and probably a punctuation mark.
	if (m/(^[A-Z])([A-Z]+)(\W?)/)
	{
		# Grab the part of the word in caps,
		# but not the first letter.
		$Buffer = $2;

		# Convert that part to lowercase.
		$Buffer =~ tr/[A-Z]/[a-z]/;

		# Build the string back together again.
		push @String, $1 . $Buffer . $3;
	}
	else
	{
		push @String, $_;
	}
}

print join (" ", @String);

Hope this helps.



Best regards,
Ramesh



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch