Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Hash not working correctly with spanish words

by qnguyen (Acolyte)
on Sep 26, 2007 at 12:59 UTC ( [id://641140]=perlquestion: print w/replies, xml ) Need Help??

qnguyen has asked for the wisdom of the Perl Monks concerning the following question:

I am having a problem with using spanish characters in my hashes. If I use a spanish word for a key, ex. dígito verificador incorrecto., it will not return the hash value. I know the hash key and value exist in my hash table. Does anyone have any suggestions?
#!/usr/bin/perl use warnings; use strict; ## LAN filename for translation my $filetwo="test2.txt"; ## Translated debug file my $fileout="out.txt"; my $linein; my $englishword; my $spanishword; my %langhash; my @arrayin; ## Open Lan file to read in the English and Spanish words open (FILEIN2, $filetwo) or die "Can't open debug file to be translat +ed.\n";; open (FILEOUT, ">$fileout") or die "Can't open output file.\n";; ## Go through each line and replace while ($linein = <FILEIN2>) { chomp $linein; @arrayin = split /:/, $linein; ##creates an array with + split function using : as separator if (exists $arrayin[0] && $arrayin[0] eq "Default") { $englishword = $arrayin[1]; } if (exists $arrayin[0] && $arrayin[0] eq "Spanish_LatinAmerica +n|es_MX") { $spanishword = $arrayin[1]; } if (exists $arrayin[0] && $arrayin[0] eq "Spanish_LatinAmerica +n|es_MX") { $langhash{$spanishword} = $englishword; } } ## while (($spanishword, $englishword) = each %langhash){ ##Pr +int entire hash tableS ## print "$englishword => $spanishword\n"; ## } close(FILEIN2); close(FILEOUT); ##while (($spanishword, $englishword) = each %langhash){ ##Print en +tire hash tableS ## print "$englishword => $spanishword\n"; ## print "$langhash{$spanishword}\n"; ## } if (exists $langhash{'dígito verificador incorrecto.'}){ print "$langhash{'dígito verificador incorrecto.'}\n"; } else { print "No match\n"; }
Test2.txt contents:
Comment:No Translation Needed Default:digit incorrect. Translate: FALSE Spanish_LatinAmerican|es_MX:dígito verificador incorrecto. Comment: This message is spoken to an operator when the "release licen +se" voice command is used. The message confirms to the operator that +the license was released. Default:Say ready. Translate: FALSE Spanish_LatinAmerican|es_MX:listo. Comment: This message is spoken to an operator when the "release licen +se" voice command is used. The message confirms to the operator that +the license was released. Default:Reverse Translate: FALSE Spanish_LatinAmerican|es_MX:Forward

Replies are listed 'Best First'.
Re: Hash not working correctly with spanish words
by halley (Prior) on Sep 26, 2007 at 13:09 UTC

    Your code has literal strings with non-ASCII characters, and your file that you are reading also has non-ASCII characters.

    What's the encoding for the source code file? What's the encoding for the text file you're reading? What's the encoding you're asking the system to use when reading that file?

    Any time you stray from ASCII, in pretty much any programming language, you will have to answer these three questions.

    --
    [ e d @ h a l l e y . c c ]

Re: Hash not working correctly with spanish words
by mk. (Friar) on Sep 26, 2007 at 13:14 UTC
    it works perfectly for me.
    have you checked the encoding of the files?! you'll have problems matching characters like 'í' if, for example, your program is utf8 and your txt is iso 8859-1.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    *women.pm
      I saved my test2.txt to ANSI encoding and it resolved the problem. I was using UTF-8 prior to the change.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://641140]
Approved by ww
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found