title: Perl - Problem with splitting columns in tab delimited text file and replacing columns with new values
body: <p>I have a tab delim. text file comprised of a number of rows and columns.  I want to change the contents of the first two columns, then write the amended file to a new file.<br>
  Before changing , the first two columns of each line look something like this:</p>
  
  <pre><code>COLUMN1:                                              
  dip:DIP-41935N|refseq:NP_056092|uniprotkb:Q96PU5    
  
  COLUMN2:    dip:DIP-48957N|uniprotkb:P49281
  </code></pre>
  
  <p>I want them to just contain the id number at the end of each column, so I want them to be as follows:</p>
  
  <pre><code>COLUMN1:        Q96PU5          
  
  COLUMN 2:       P49281
  </code></pre>
  
  <p>I have split the lines at the tab to get the individual columns.  Then split the first 2 columns to get the required ID number ($prot_id).  Then I have tried substituting the ID for the contents of columns 1 and 2.  However the output in the changed file is not as I expect.  It instead looks something like this:</p>
  
  <pre><code>  COLUMN1:                                           
  Q96PU5|refseq:NP_056092|uniprotkb:Q96PU5    
  
  COLUMN 2:
  P49281|uniprotkb:P49281
  </code></pre>
  
  <p>Just the first part of the columns has been substituted.  I have been playing around with this for hours and cannot figure out what I'm doing wrong.  Any help greatly appreciated. 
  My code is as follows: </p>
  
  <pre><code>#!/usr/bin/perl  
  
  use warnings;
  use strict;
  
  
  my $file = 'DIP.txt';
  
  open(INFILE, $file) or die "Can't open file: $!\n";
  open(my $outfile, '&gt;', 'DIP_changed.txt'); 
  my @lines = &lt;INFILE&gt;;
  
  
  foreach $_ (@lines) {
      my @columns = split('\t', $_);
  
              my $col1 = $columns[0];
              my $col2 = $columns[1];
  
  
              my @split_col1 = split ('uniprotkb:', $col1);
              my @split_col2 = split ('uniprotkb:', $col2);
  
              my $prot_id1 = $split_col1[length(@split_col1)];
              my $prot_id2 = $split_col2[length(@split_col2)];
  
              print $prot_id1, "\n";
  
               s/$col1/$prot_id1/;
               s/$col2/$prot_id2/;
  
              print {$outfile} $_; 
  }
  
  
  
  exit;
  </code></pre>