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

better code

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
Hello,

Can anyone think of a better way of addressing the following problem. I have a form and any name can be submitted - including ones with accents and special characters - the names are then used later on in the script to generate a latex template using a system call. However, latex handles special characters differently, for example ü is wriiten as \'"{u}. Therefore, as a enter the function i want to substutite any accent characters with the equilavant code. This is what i have come up with but i'm sure there is a better way of doing this:

my @special_characters = ("ü","Ü","ç","Ç","ä","Ä","é","É");
my %character_map = ( "ü", "\\\"{u}",
"Ü", "\\\"{U}",
"ç", "\\c{c}",
"Ç", "\\c{C}",
"ä", "\\\"{a}",
"Ä", "\\\"{A}",
"é", "\\'{e}",
"É", "\\'{E}"); foreach my $el (@special_characters){ if ($author =~ /($el)/){
my $letter = $character_map{$el};
$author =~ s/$1/$letter/g;
}
}

has anyone any suggestions?

jim
 
The 'foreach' loop could be modifed thus


foreach my $el (@special_characters){
$author =~ s/$el/$character{$el}/g;
}

HTH
C "Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
Things to make the code cleaner:
Code:
my %character_map = (
    "Ü" => qq{\\"{U}},
    "ç" => qq{\\c{c}},
    etc...
);
foreach( sort keys %character_map ) {
   $author =~ s/$_/$character_map{$_}/g;
}
You don't need a separate array of @special_characters.
Cheers, Neil
 
ps: you can use q{\"{U}} instead of qq{\\"{U}} as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top