title: How to search and replace using hash with Perl
body: <p>I'm new to Perl and I'm afraid I am stuck and wanted to ask if someone might be able to help me.</p>
  
  <p>I have a file with two columns (tab separated) of oldname and newname.</p>
  
  <p>I would like to use the oldname as key and newname as value and store it as a hash.</p>
  
  <p>Then I would like to open a different file (gff file) and replace all the oldnames in there with the newnames and write it to another file. </p>
  
  <p>I have given it my best try but am getting a lot of errors.</p>
  
  <p>If you could let me know what I am doing wrong, I would greatly appreciate it.</p>
  
  <p>Here are how the two files look:</p>
  
  <p>oldname newname(SFXXXX) file:</p>
  
  <pre><code>genemark-scaffold00013-abinit-gene-0.18 SF130001
  augustus-scaffold00013-abinit-gene-1.24 SF130002
  genemark-scaffold00013-abinit-gene-1.65 SF130003
  </code></pre>
  
  <p>file to search and replace in (an example of one of the lines):</p>
  
  <pre><code>scaffold00013   maker   gene    258253  258759  .   -   .   ID=maker-scaffold00013-augustus-gene-2.187;Name=maker-scaffold00013-augustus-gene-2.187;
  </code></pre>
  
  <p>Here is my attempt:</p>
  
  <pre><code>#!/usr/local/bin/perl
  use warnings;
  use strict;
  
  my $hashfile = $ARGV[0];
  my $gfffile  = $ARGV[1];
  my %names;
  my $oldname;
  my $newname;
  
  if (!defined $hashfile) {
      die "Usage: $0 hash_file gff_file\n";
  }
  
  if (!defined $gfffile) {
      die "Usage: $0 hash_file gff_file\n";
  }
  
  ###save hashfile with two columns, oldname and newname, into a hash with oldname as key and newname as value.
  
  open(HFILE, $hashfile) or die "Cannot open $hashfile\n";
  
  while (my $line = &lt;HFILE&gt;) {
      chomp($line);
      my ($oldname, $newname) = split /\t/;
      $names{$oldname} = $newname;
  }
  
  close HFILE;
  
  ###open gff file and replace all oldnames with newnames from %names.
  
  open(GFILE, $gfffile) or die "Cannot open $gfffile\n";
  
  while (my $line2 = &lt;GFILE&gt;) {
      chomp($line2);
  
      eval "$line2 =~ s/$oldname/$names{oldname}/g";
  
      open(OUT, "&gt;SFrenamed.gff") or die "Cannot open SFrenamed.gff: $!";
  
      print OUT "$line2\n";
  
      close OUT;
  }
  
  close GFILE;
  </code></pre>
  
  <p>Thank you!</p>