[Solved] Perl Column comparison in two files


I’m going to guess you already have code for reading each of these files, and just need to parse out the values and organize them.

The design part is coming up with a key that is unique for each start,end pair, but not hard to work with. Since start and end are numbers, this should be pretty easy:

our %matchups ;

sub process
{
  my ($lst_)= @_ ;
  for ( @$lst_ ) {
    my ($strt,$endn)= /\d+\w+(\d+)\w+(\d+)/ ;
    next unless $strt && $endn ;
    my $key= "${strt}_$endn" ;
    $matchups{$key}[0]= $_ ;
    $matchups{$key}[1] ++ ;
  }
}

sub outputmatch
{
  my ($dest,$multi)= @_ ;
  # open file
  for ( values %matchups ) {
    print $OUT $_->[0] if ( $_->[1] > 1 ) == $multi ;
  }
}

{
  process(@listfrom1txt) ;
  process(@listfrom2txt) ;

  outputmatch( "common.txt", 1 ) ;
  outputmatch( "uniq.txt", 0 ) ;
}

So here we make a key which is start_end, and then we build a data structure inside the hash which is an array of two elements. The first element is the original line, the second is the count of how many times we’ve seen this entry.

If a line is unique, the count will be 1; if it’s not, then it will be greater than 1.

2

solved Perl Column comparison in two files