in reply to Match two files using regex

Even though I answered this on StackOverflow, I'll paste my solution here for completeness purposes:

Here's one way using split() hackery:

#!/usr/bin/perl use strict; use warnings; my $f1 = 'file1.txt'; my $f2 = 'file2.txt'; my @pdb; open my $pdb_file, '<', $f2 or die "Can't open the PDB file $f2: $!"; while (my $line = <$pdb_file>){ chomp $line; push @pdb, $line; } close $pdb_file; open my $shifts_file, '<', $f1 or die "Can't open the SHIFTS file $f1: $!"; while (my $line = <$shifts_file>){ chomp $line; my $pdb_line = shift @pdb; # - inner split: get the third element from the $pdb_line # - outer split: get the first element (character) from the # result of the inner split my $criteria = (split('', (split('\s+', $pdb_line))[2]))[0]; # - compare the 2nd element of the file1.txt line against # the above split() operations if ((split('\s+', $line))[1] eq $criteria){ print "$pdb_line\n"; } else { print "**** >$pdb_line< doesn't match >$line<\n"; } }

Files:

file1.txt (note I changed line two to ensure a non-match worked):

1 H 35 1 A 22 1 H 20

file2.txt:

A 1 HB2 MET 1 A 2 CA MET 1 A 3 HA MET 1

Output:

./app.pl A 1 HB2 MET 1 ****>A 2 CA MET 1< doesn't match >1 A 22< A 3 HA MET 1

-stevieb

Replies are listed 'Best First'.
Re^2: Match two files using regex
by Anonymous Monk on Jun 02, 2015 at 18:06 UTC
    It was correct and I appreciate you posting it as I got the output I needed however, I would just like to see where I went wrong with my code.

      Gotcha. Just for forward-going, it's always best to state up-front that you've cross-posted and that you're just looking for further advice.

      No harm done. I'll have another look at your original code later on if nobody else gets a chance to debug it.

      Cheers,

      -stevieb

        Thanks again!
Re^2: Match two files using regex
by chemshifts (Initiate) on Jun 02, 2015 at 18:08 UTC
    It was correct and I appreciate you posting it as I got the output I needed however, I would just like to see where I went wrong with my code.
Re^2: Match two files using regex
by Anonymous Monk on Jun 03, 2015 at 05:18 UTC

    Actually, it doesn't look correct. The output lines are supposed to begin with a number, not a letter, and the output is missing the values from the third column of the first file, which should be the fourth column of the output.

      I was able to get the correct output by adding these variables to the print command.