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

I need some help with building a hash table. I am trying to build a hash table with spanish word as my key and english word as my value. The following code works great when my file contains the line for default and Spanish_LatinAmerican|es_MX. But, as you can see below, I need to use the PRON as an indicator for my english word. Then there are sometimes two Spanish_LatinAmerican|es_MX that follows. Also, how can i check if the hash key already exists? If it exists, i would like to have the value printed with the english translation and a warning message. Does anyone have any suggestions how i can rewrite this?
open (FILEIN2, $filetwo) or die "Can't open file.\n"; open (FILEOUT8, ">$hashtable") or die "Can't open debug file to be tr +anslated.\n"; ## Go through each line and replace while (<FILEIN2>) { next unless ( /.:\s*\S/ ); my ( $lang, $text ) = split /:/, $_, 2; if ( $lang eq "Default" ) { $englishword = trim( $text ); # takes care of chomping } elsif ( $lang eq "Spanish_LatinAmerican|es_MX" ) { $spanishword = trim( $text ); $langhash{$spanishword} = $englishword; } } while (($spanishword, $englishword) = each %langhash){ ## Tes +t print of entire hash table print FILEOUT8 "$spanishword => $englishword\n"; } close(FILEIN2); close(FILEOUT8);
Comment: No Translation Needed Default: 1 Translate: FALSE Spanish_LatinAmerican|es_MX: uno Comment: No Translation Needed Default: 3 Translate: FALSE Spanish_LatinAmerican|es_MX: tres Comment: No Translation Needed Default: <Spell> {ASSIGNMENT[WORKID=PICKS[].WORKID].ROUTE} </Spell> Translate: FALSE Spanish_LatinAmerican|es_MX: <Spell> {ASSIGNMENT[WORKID=PICKS[].WORKID +].ROUTE} </Spell> Phonetic: range Pron: range Spanish_LatinAmerican|es_MX: rango Display: range Spanish_LatinAmerican|es_MX: rango Phonetic: range Pron: range Spanish_LatinAmerican|es_MX: listo Display: ready Spanish_LatinAmerican|es_MX: listo Comment: This prompt occurs at the end of an assignment telling the op +erator the assignment is complete and if needed, also tells the opera +tor shorts have occured. Shorts are when an operator picks less items + at a location then they were supposed to. Default: Picking complete. Spanish_LatinAmerican|es_MX: Selección completa.

Replies are listed 'Best First'.
Re: Question on building hash table
by jdporter (Paladin) on Nov 08, 2007 at 01:28 UTC
    how can i check if the hash key already...

    exists?