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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Text Translation

Status
Not open for further replies.
Mar 11, 2004
127
0
0
GB
Hi,

I'm still relatively new to Perl, but have managed to put together some scripts for work.

The latest one I'm making is taking a CSV file of data, and creating an XML output.

One of the fields in in all UPPERCASE, and I need to reformat it to Upper Then Lower.

For example the data coming in is like:
LONDON GATWICK
BELFAST
SHETLANd ISLAND
PRAGUE

And I need output to be:
London Gatwick
Belfast
Shetland Island
Prague

I have tried various tr statements, and have nearly got a workable solution using an if (pattern match), but I'm still failing to get the UPPERCASE on the second word.

If someone could point me in the direction I need to go I would be really greatful, I'm pulling my hair out at the moment as this is the last step in the script.

Thanks in advance!
Anthony
 
Code:
@name=split(/ /, $name);
foreach (@name) {
  $_=ucfirst($_);
}
print join (" ", @name);

sub ucfirst {
  ($prop)=@_;
  @letters=split (//, $prop);
  foreach (@letters) {
    $_=lc($_);
  }
  $letters[0]=uc($letters[0]);
  return join ('', @letters);
}
Not tested, but should give you something to work on

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@data[/blue] = [red]([/red]
	[red]'[/red][purple]LONDON GATWICK[/purple][red]'[/red],
	[red]'[/red][purple]BELFAST[/purple][red]'[/red],
	[red]'[/red][purple]SHETLANd ISLAND[/purple][red]'[/red],
	[red]'[/red][purple]PRAGUE[/purple][red]'[/red],
[red])[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$string[/blue] [red]([/red][blue]@data[/blue][red])[/red] [red]{[/red]
	[blue]$string[/blue] =~ [red]s{[/red][purple]([purple][b]\S[/b][/purple]+)[/purple][red]}[/red][red]{[/red][purple][purple][b]\u[/b][/purple][purple][b]\L[/b][/purple][blue]$1[/blue][/purple][red]}[/red][red]g[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$string[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller

PS,
Paul: ucfirst is a built in function.
 
Code:
$name =~ s/\b(\w)(\w+)/uc($1).lc($2)/eg;

The [tt]/b[/tt] matches a zero-width word boundary, including the beginning of the string.

The [tt]/e[/tt] modifer permits code in the substitution, so we can use the [tt]lc()[/tt] and [tt]uc()[/tt] functions with the . operator.

The [tt]/g[/tt] modifier runs the substitution multiple times.

This code also correctly handles input like St.Andrews.

Yours,

fish



["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
Thanks very much guys, I will give them a go and see what works best for my situation.

Anthony
 
In the end I took millerH's suggestion, but all help is appreciated. This opened the door for me to make a few extra additional changes to the formatting of the data which has made me much happier with the output.

Once again, tek-tips shines its light on my darkest hour. Thank you!

Anthony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top