title: Perl: Comparing two string of two files
body: <p>I have two CSV files. Both have a column that contains the same data, with the difference that one file contains more data in that column than the other.</p>
  
  <p>I want to just print out the rows of <code>file2</code> in which contains the same string in that column as in the other file.</p>
  
  <p>For example: </p>
  
  <p><strong>file1</strong></p>
  
  <pre><code>App_Int1     SID_bla1
  App_Int2     SID_bla2
  App_Int_4    SID_bla4
  </code></pre>
  
  <p><strong>file2</strong></p>
  
  <pre><code>SID_bla1     hello     bye    ...
  SID_bla2     good      bad    ...
  SID_bla5     hey       ho     ....
  SID_bla4     hi        cheers ...
  </code></pre>
  
  <p>And I want the output to be like this</p>
  
  <pre><code>SID_bla1     hello     bye    ...
  SID_bla2     good      bad    ...
  SID_bla4     hi        cheers ...
  </code></pre>
  
  <p>Because file1 doesn't contain <code>SID_bla5</code>, the row with <code>SID_bla5</code> will not be printed.</p>
  
  <p>Here is my code but it doesn't work, can somebody give me some hints?</p>
  
  <pre><code>#!C:\Perl\bin\perl
  use strict;
  use warnings;
  
  my $file = $ARGV[0] || die "Need to get CSV file on the command line\n";
  my $mystring = "";
  
  open(my $data, '&lt;', $file) || die "Could not open '$file' $!\n";
  my $newfile = "fooNew3.txt";
  open(FILE2, "&gt;", $newfile) || die "Could not open file";
  
  my $file2 = "export.txt";
  open(my $data2, '&lt;', $file2) || die "Could not open '$file2' $!";
  
  my $mystring2 = "";
  my $line2;
  my %filehash;
  my @fields2 = "";
  
  while ($line2 = &lt;$data2&gt;) {
    chomp $line2;
  
    @fields2 = split(";", $line2);
    while (my $line = &lt;$data&gt;) {
      chomp $line;
  
      my @fields = split(";", $line);
      if ($filehash{ $fields2[0] } eq $fields[1]) {
        # if the first column of file2 is identical with the second column of file1
        # then output the identical string and the second column of file2
        # which belongs to the first column of file2 (which is the identical string)
  
        print FILE2 join ';', "$fields[1]; $filehash{$fields2[0]} $fields2[1] \n";
      }
  </code></pre>
  
  <p>What would be wrong with this?</p>
  
  <pre><code>  if ($fields2[0] eq $fields[1] {
      print $fields2[0] $fields2[1] $fields2[2];
    }
  </code></pre>