Newbie: Help Substituting from a Hash

Discussion in 'Perl Misc' started by Elisa Francesca Roselli, Sep 16, 2005.

  1. 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
     
    Elisa Francesca Roselli, Sep 16, 2005
    #1
    1. Advertising

  2. 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'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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Sep 16, 2005
    #2
    1. Advertising

  3. 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.
     
    Brian McCauley, Sep 16, 2005
    #3
  4. Brian McCauley wrote:

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


    Er no,

    $Fr =~ s/([`'^][aeiou])/ $q_format{$1} || $1 /eg;
     
    Brian McCauley, Sep 16, 2005
    #4
  5. 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
     
    Elisa Francesca Roselli, Sep 19, 2005
    #5
  6. 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
     
    Elisa Francesca Roselli, Sep 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    2
    Views:
    400
    Young Seeker
    Dec 29, 2003
  2. Christian Schlichtherle
    Replies:
    8
    Views:
    476
    John Currier
    Jul 5, 2005
  3. lugal
    Replies:
    2
    Views:
    312
    Peter Otten
    Mar 23, 2005
  4. rp
    Replies:
    1
    Views:
    540
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
Loading...

Share This Page