Yes, it was a problem with greediness, thank you. I ran into a problem when I ran my code with warning on. I receive the error 'use of uninitialized value in concactanation' and I get no value in my printout. I know it's because the hash key and value are not present in my hash table. 1. Is there a way to ignore the substitution if the hash value does not exist? 2. Also, is there a way to ignore leading and trailing whitespaces in my hash value when using a match or substituion or would it be more effecient to remove the whitespaces and rewrite the hash table.
use warnings; #!/usr/bin/perl ##Objective is to translate Spanish Prompts to English Prompts ## FIL filename for translation $fileone="COM_211_RAD_07A_ES_MX.FIL"; ## LAN filename for translation $filetwo="VoiceLink only Soriana.lan"; ## Debug file to be translated $filetotranslate = "test.txt"; ## English to Spanish translations file $fileout="out.txt"; $fileout2="out2.txt"; ## Debug file with translated from Spanish to English $fileout3="debug.txt"; ## Open the lan file to read in the English and Spanish words ##open (FILEIN, $fileone) or die "Can't open first file.\n";; ## Read in each line and create a hash table ##while ($linein = <FILEIN>) { ## chomp $linein; ## @arrayin = split /\|/, $linein; ##creates an array wit +h split function using | as separator ## $englishword = $arrayin[0]; ## $spanishword = $arrayin[1]; ## print "$englishword -> $spanishword\n"; ## $langhash{$spanishword} = $englishword; ## print "$langhash{$spanishword}\n"; ##} ##close(FINEIN); ## 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, ">$fileout2") 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 ($arrayin[0] eq "Default") { $englishword = $arrayin[1]; } if ($arrayin[0] eq "Spanish_LatinAmerican|es_MX") { $spanishword = $arrayin[1]; } if ($arrayin[0] eq "Spanish_LatinAmerican|es_MX") { $langhash{$spanishword} = $englishword; ## print FILEOUT "$englishword -> $spanishword\n"; }} while (($spanishword, $englishword) = each %langhash){ ##Prin +t entire hash table print FILEOUT "$spanishword=>$englishword\n"; } close(FILEIN2); close(FILEOUT); ## Open debug file to convert from English to Spanish open (FILEIN3, $filetotranslate) or die "Can't open debug file.\n"; open (FILEOUT3, ">$fileout3") or die "Can't open output file.\n"; ## Go through each line and replace while ($linein3 = <FILEIN3>) { if ($linein3 =~ s/##\s*([^#]+?)\s*##/##$langhash{$1}##/){ print FILEOUT3 "$linein3"; } else { print FILEOUT3 "$linein3"; }}

In reply to Re^2: substitution not working correctly by qnguyen
in thread substitution not working correctly 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.