Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper text

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I had a database field containing many book titles all in uppercase. The titles were required to be propered.
I wrote a dirty solution which got the job done but there must be a more elegant solution.
Any suggestions?
Code:
$OLDBOOKS[$x]=lc($OLDBOOKS[$x]);
$OLDBOOKS[$x]=ucfirst($OLDBOOKS[$x]);
$LENF=length($OLDBOOKS[$x]);
for($y=0; $y<$LENF; ++$y){
	$test=substr($OLDBOOKS[$x],$y, 1);
	if($test eq ' '){
		substr($OLDBOOKS[$x],$y+1,1) = ucfirst(substr($OLDBOOKS[$x],$y+1,1));
	}
}

Keith
 
audiopro,

What value $OLDBOOKS[$x] can contain?
Try with this code..
Code:
my $oldbook = $OLDBOOKS[$x] ;
$oldbook =~ s/\s+//g ;
my $newbook = ucfirst($oldbook);
#print $newbook ;

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
The book titles are all stored in upper case and need to be displayed propered to make them look pretty.
Your code leaves them in upper case, what should the substitution do?
Here are some before and after pairings.

Code:
MY WAY - My Way
ANOTHER BRICK IN THE WALL - Another Brick In The Wall
FLY ME TO THE MOON - Fly Me To The Moon
SOMEWHERE MY LOVE - Somewhere My Love

My dirty solution got the job done but there has to be a better way.

Keith
 
Code:
use strict;
use warnings;

while (<DATA>) {
   $_ = lc $_;
   s/(\b\w)/\U$1\E/g;
   print $_;
} 

__DATA__
MY WAY
ANOTHER BRICK IN THE WALL
FLY ME TO THE MOON
SOMEWHERE MY LOVE

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Actually, the lower casing isn't needed.
Code:
use strict;
use warnings;

while (<DATA>) {
   s/(\B\w)/\L$1\E/g;
   print;
}

__DATA__
MY WAY
ANOTHER BRICK IN THE WALL
FLY ME TO THE MOON
SOMEWHERE MY LOVE

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Weird. I get
Code:
My Way
Another Brick In The Wall
Fly Me To The Moon
Somewhere My Love
What version of Perl are you running? I'm on Activestate 5.10.0

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top