title: In a file/array, search for hash key, and replace it with the hash value, do this for all hash keys/values
body: <p>I've searched around the site and surprisingly I can't seem to find something that will work for my particular problem.  So I figured I'd post it and see how some of you more experienced programmers can address with problem.</p>
  
  <p>I have a spreadsheet like text file (many lines with tab delimited columns), that I would like to search through for certain labels (ex scaffold1253.1_size81005.6.32799_7496) and replace them with more simplified labels (ex scaffold1253.1a). These labels are only in the first column of the text file. I've already written the script such that I have a hash with the old labels as keys corresponding to the new labels as their respective values. This hash has about 26000 lines. So essentially I'd like to take the hash keys 1 by 1, search for them in the text file, and replace them with their respective hash values. </p>
  
  <p>I have a pretty good server availible so if its too complicated to make it first column specific to speed up the process then thats ok. </p>
  
  <p>THis is what I have so far:</p>
  
  <pre><code> use warnings;  
  
  
  
  $gtf = './Hc_genome/Hc_rztk_1+2+8+9.augustus.gtf'; 
      open(FASTAFILE2, $gtf);
      @gtfarray = &lt;FASTAFILE2&gt;;
      #print @gtfarray;
  
  
  my %hash;
  while (&lt;&gt;)
  {
     chomp;
     my ($key, $val) = split /\t/;
     $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
  }
  
  #print %hash;
  
  while (my ($find, $replace) = each %hash) {
      foreach (@gtfarray){
          $_ =~ s/$find/$replace/g;
          push @newgtf, $_;   
      }
  }
  print @newgtf;
  </code></pre>
  
  <p>This code doesn't seem to work as it doesn't complete. I'm pretty sure it's a problem with the foreach loop structure. Sorry I don't know of any other way to do this. Does anyone have a better way to run through this file and conduct the replacement?</p>
  
  <p>Any input would be greatly appreciated!
  Thanks,</p>
  
  <p>Andrew</p>
  
  <p>@DVK</p>
  
  <p>Here is the full script with your mods that runs into syntax errors with your while loop, any idea why it's not accepting it? Thanks again!</p>
  
  <pre><code>use warnings;  
  
  $gtf = './Hc_genome/Hc_rztk_1+2+8+9.augustus.gtf'; 
      open(FASTAFILE2, $gtf);
  
  my %hash;
  while (&lt;&gt;){
      chomp;
      my ($key, $val) = split /\t/;
      $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
  }
  
  
  while $line (&lt;FASTAFILE2&gt;){
      my @fields = split(/\t/, $line);
      # If you only care about first column, don't need the foreach loop below;
      #    just do the loop insides on $fields[0]
      foreach my $field (@fields) {
          $field = $hash{$field} if exists $hash{$field};
          print $outfile "$field\t"; # Small bug - will print training \t
      }
      print $outfile "\n"
  }
  
  __END__
  </code></pre>
  
  <p>Here is the syntax error:
  perl gtf_mod2.pl &lt;./Hc_genome/header_file.txt
  syntax error at gtf_mod2.pl line 14, near "while $line "
  syntax error at gtf_mod2.pl line 23, near "}"
  Execution of gtf_mod2.pl aborted due to compilation errors.</p>