title: How to replace string dynamically using perl script
body: <p>I am trying to solve below issues.</p>
  
  <p>I have 2 files. Address.txt and File.txt. I want to replace all A/B/C/D (File.txt) with corresponding string value (Read from Address.txt file) using perl script. It's not replacing in my output file. I am getting same content of File.txt.
  I tried below codes.</p>
  
  <p>Here is Address.txt file</p>
  
  <pre><code>A,APPLE
  B,BAL
  C,CAT
  D,DOG
  E,ELEPHANT
  F,FROG
  G,GOD
  H,HORCE
  </code></pre>
  
  <p>Here is File.txt</p>
  
  <pre><code>A B C
  X Y X
  M N O
  D E F
  F G H
  </code></pre>
  
  <p>Here is my code :</p>
  
  <pre><code>use strict;
  use warnings;
  open (MYFILE, 'Address.txt');
  
  foreach (&lt;MYFILE&gt;){
      chomp;
      my @data_new = split/,/sm;
      open INPUTFILE, "&lt;", $ARGV[0] or die $!;
      open OUT, '&gt;ariout.txt' or die $!;
        my $src = $data_new[0];
      my $des = $data_new[1];
  
        while (&lt;INPUTFILE&gt;) {
            # print "In while :$src \t$des\n";
            $_ =~ s/$src/$des/g;
            print OUT $_; 
        }
  
        close INPUTFILE;
        close OUT;
        # /usr/bin/perl -p -i -e "s/A/APPLE/g" ARGV[0];
  }
  close (MYFILE);
  </code></pre>
  
  <p>If i Write <strong>$_ =~ s/A/Apple/g;</strong></p>
  
  <p>Then output file is fine and A is replacing with "Apple". But when dynamically coming it's not getting replaced.</p>
  
  <p>Thanks in advance. I am new in perl scripting language . Correct me if I am wrong any where. </p>
  
  <p><strong>Update 1:</strong> I updated below code . It's working fine now. My questions Big O of this algo.
  Code :</p>
  
  <pre><code>#!/usr/bin/perl
  use warnings;
  use strict;
  
  open( my $out_fh, "&gt;", "output.txt" ) || die "Can't open the output file for writing: $!";
  
  open( my $address_fh, "&lt;", "Address.txt" ) || die "Can't open the address file: $!";
  my %lookup = map { chomp; split( /,/, $_, 2 ) } &lt;$address_fh&gt;;
  
  open( my $file_fh, "&lt;", "File1.txt" ) || die "Can't open the file.txt file: $!";
  while (&lt;$file_fh&gt;) {
      my @line = split;
      for my $char ( @line ) {
          ( exists $lookup{$char} ) ? print $out_fh " $lookup{$char} " : print $out_fh " $char ";
      }
      print $out_fh "\n";
  }
  </code></pre>