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.

In reply to Question on building hash table by qnguyen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.