Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie: Help Substituting from a Hash

Reply
Thread Tools

Newbie: Help Substituting from a Hash

 
 
Elisa Francesca Roselli
Guest
Posts: n/a
 
      09-16-2005
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'accs 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
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-16-2005
Elisa Francesca Roselli wrote:
> 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'accs 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
--
Josef Mllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-16-2005

Josef Moellers wrote:
> Elisa Francesca Roselli wrote:
>
>> %q_format = ('\\`e' => '', #plotting \`e to
>> '\\\'e' => '', #plotting \'e to
>> '\\^e' => ';', #plotting \^e to
>> '\\`a' => '', #plotting \`a to
>> '\\^o' => '' ); #plotting \^o to
>>

> 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.

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-16-2005


Brian McCauley wrote:

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


Er no,

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

 
Reply With Quote
 
Elisa Francesca Roselli
Guest
Posts: n/a
 
      09-19-2005
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
 
Reply With Quote
 
Elisa Francesca Roselli
Guest
Posts: n/a
 
      09-19-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Substituting AOL Wav files with MSN Wav files Thaqalain Computer Support 6 07-07-2005 08:17 PM
Substituting Java API classes for enhanced functionality (e.g. java.io.File) Christian Schlichtherle Java 8 07-04-2005 11:20 PM
substituting values in property file Andy Fish Java 2 12-29-2003 05:11 PM



Advertisments