perl: array matching

Hi,

I have two files like this

file 1:

xxtcgtatccgaggga
cgcgcgggggagg
jjsjjjjsjjjdtcgtat
aaaaaaacccaaan
ggtcgtatffaadda
gggctggalllslllssdkk
file 2:

tcgtat
gctgga

I want to 1) match each element of file2 to each element of file1.
2) delete all the matched alphabets and subsequent letters in the element of file1.

so the output should be

xx
cgcgcgggggagg
jjsjjjjsjjjd
aaaaaaacccaaan
gg
gg

I tried with regular expressions but couldnt figure out how to match elements of one array with another array.

If possible please provide the code in perl because I just started learning perl.

thanks in advance:)

#!/usr/bin/perl
open A, "$ARGV[1]";
open B, "$ARGV[0]";
chomp(@x=<A>);
local $/;
$_=<B>;
for $x (@x){
  s/$x.*//g;
}
print;

Run it like this: ./script.pl file1 file2

1 Like

Hi Bartus11,

thanks a lot. its working perfectly. But I have couple of questions to understand the code clearly.

  1. what was the purpose pf 'local $/' on line 5?
  2. In the code $_ = <B> , is each line of the file read individually and assigned to $_? I tried to assign the <B> to an array too just like A, but this part i was missing.

thanks again.:b:

It is input record separator variable. Unsetting it is causing the whole file to be loaded into $_ in the next line of code.