Newbie: Help Substituting from a Hash

  • Thread starter Elisa Francesca Roselli
  • Start date
E

Elisa Francesca Roselli

I have some strings in French that have had all the diacritical marks
removed and substituted with special multi-character codes.

I need to get these strings output as natural language, with their
diacriticals restored.

What I'm trying to do is to use a hash, with the special code as the
hash key and the natural character as the value. Example:

%q_format = ('\\`e' => 'è', #plotting \`e to è
'\\\'e' => 'é', #plotting \'e to é
'\\^e' => 'ê;', #plotting \^e to ê
'\\`a' => 'à', #plotting \`a to à
'\\^o' => 'ô' ); #plotting \^o to ô


So lets say I have a French-language string in a scalar $Fr. I want to
do something like:

foreach $key(keys %q_format) {
$Fr =~s/$q_format{key}/$q_format{value}/g;
}

to substitute the key with the value. So if $Fr contains

"Vous n'avez pas l'acc\`es en \'ecriture sur "%S"

I want to see

"Vous n'avez pas l'accès en écriture sur "%S"
But that isn't working. The string outputs exactly as it went in.

What am I doing wrong?

Many thanks for your patience, to all who can assist,

Elisa Francesca Roselli
Ile de France
 
J

Josef Moellers

Elisa said:
I have some strings in French that have had all the diacritical marks
removed and substituted with special multi-character codes.

I need to get these strings output as natural language, with their
diacriticals restored.

What I'm trying to do is to use a hash, with the special code as the
hash key and the natural character as the value. Example:

%q_format = ('\\`e' => 'è', #plotting \`e to è
'\\\'e' => 'é', #plotting \'e to é
'\\^e' => 'ê;', #plotting \^e to ê
'\\`a' => 'à', #plotting \`a to à
'\\^o' => 'ô' ); #plotting \^o to ô


So lets say I have a French-language string in a scalar $Fr. I want to
do something like:

foreach $key(keys %q_format) {
$Fr =~s/$q_format{key}/$q_format{value}/g;
}

to substitute the key with the value. So if $Fr contains

"Vous n'avez pas l'acc\`es en \'ecriture sur "%S"

I want to see

"Vous n'avez pas l'accès en écriture sur "%S"
But that isn't working. The string outputs exactly as it went in.

What am I doing wrong?

Remember that $q_format{...} is the value, while ... is the key.
If you replace $q_format{key} with $key and $q_format{value} with
$q_format{$key}, it should work.

foreach $key (keys %q_format) {
$Fr =~s/$key/$q_format{$key}/g;
}

Josef
 
B

Brian McCauley

Josef said:
Remember that $q_format{...} is the value, while ... is the key.
If you replace $q_format{key} with $key and $q_format{value} with
$q_format{$key}, it should work.

foreach $key (keys %q_format) {
$Fr =~s/$key/$q_format{$key}/g;
}

You are forgetting that ^ is meta in regex.

Also $key is being declared in an inappropriately wide scope:

foreach my $key (keys %q_format) {
$Fr =~ s/\Q$key/$q_format{$key}/g;
}

Note also that conventional wisdom would be to avoid the loop

$Fr =~ s/([^`'][aeiou])/ $q_format{$1} || $1 /eg;

You should consdier using conventional entities like 'é' rather
than your home-rolled convention.
 
E

Elisa Francesca Roselli

Josef Moellers a écrit :
Remember that $q_format{...} is the value, while ... is the key.
If you replace $q_format{key} with $key and $q_format{value} with
$q_format{$key}, it should work.

foreach $key (keys %q_format) {
$Fr =~s/$key/$q_format{$key}/g;
}

Thanks for the clarifaication, it's now working. The only problem is
that I had to use double-quotes instead of single and all hell broke
loose with the despecifications. Why on earth does it take no less than
SIX backslashes to get a statement like \^e to be read correctly?

EFR
Ile de France
 
E

Elisa Francesca Roselli

Brian McCauley a écrit :
You should consdier using conventional entities like 'é' rather
than your home-rolled convention.
Yes, that would be nice. Unfortunately I have no control over the input
code. I'm just the poor technical writer who is trying to extract some
readable strings for the translation tables.

EFR
Ile de France
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top