in reply to match pattern from two different file

foreach my $line1(@array1) { foreach my $line2(@array2) if { $line1=~ /^$pattern/; $line2=~ /^$pattern/; $line1==$line2; { print $line1; } }

Your code has syntax error.

moreover eq is for comparing strings. == is for comparing numbers.

Adding code:
#!/usr/bin/perl use strict; use warnings; open(FH, "<","./file1.txt") or die "Can't Open File1\n"; open(BH, "<","./file2.txt") or die "Can't Open File2\n"; my $pattern='^LOC_Os0[1-7]g[0-9]*.[0-9]\s'; while(my $line1 = <FH>) { chomp($line1); #print "Line1:$line1\n"; if( $line1=~ /$pattern/) { while(my $line2 = <BH>) { chomp($line2); if($line2 =~ /$pattern/) { if($line1 eq $line2) { print "Matches: ". $line1; } else { print "nothing to print\n"; } } } seek(BH,0,0); } } close(FH); close(BH);

file1.txt

LOC_Os01g01010.1 3017 : PAS 2993,3017 : AGTAAA AAATGGAGGAATTCTGCCA LOC_Os01g01010.2 2218 : uORF 7,129 : ATG AAGAAGACGAGACGACGACTTCCCCACTA +GGAAACACGACGGAGGCGGAGATGATC

file2.txt

LOC_Os01g01010.1 : PS51373 HIPIP High potential iron-sulfur proteins f +amily profile. 2139 - 2208 AATGATTTATCATGTGAGGTGAAAGA-----AGAGCACCGGG +TGAACAGTTACACAAGAA L=-1 GAAAGTCCAAAAGCA LOC_Os01g01010.2 : PS00022 EGF_1 EGF-like domain signature 1. 298 - 30 +9 CtCccTtcGTtC L=(-1 LOC_Os01g01010.2 2218 : uORF 7,129 : ATG AAGAAGACGAGACGACGACTTCCCCACTA +GGAAACACGACGGAGGCGGAGATGATC

All is well

Replies are listed 'Best First'.
Re^2: match pattern from two different file
by Anonymous Monk on Jun 20, 2014 at 07:53 UTC

    thank u code but it retrieve "nothing to print" multiple times
    I also tried again by using split().but it also print same as ur code
    newly written code

    #!/usr/bin/perl use strict; use warnings; open(FH, "outputps_scan_chr1_.out") or die "Can't Open File1\n"; my @array1=<FH>; close(FH); open(BH, "chr1_1.txt") or die "Can't Open File2\n"; my @array2=<BH>; close(BH); my $pat='qr/^LOC_Os0.../'; foreach my $line1(@array1) { my @array3= split(/ /, $line1); foreach my $line2(@array2) { my @array4= split(/ /, $line2); foreach my $line3(@array3) { foreach my $line4(@array4) { $line3=~ $pat; $line4=~ $pat; if ($line3 eq $line4) { print $line3; } else { print "nothing to right\n"; } } } } }
Re^2: match pattern from two different file
by vinoth.ree (Monsignor) on Jun 20, 2014 at 08:02 UTC

    Hey do you want to match only the pattern LOC_Os0[1-7]g[0-9]+.[0-9] in each line from both the files? or entire line? As your first post matches the entire line, that why I was checking entire line match. Please correct me if i am wrong.


    All is well

      yes I only want LOC_Os0[1-7]g[0-9]+.[0-9] from both files

      yes I only want LOC_Os01-7g0-9+.0-9 from both files

        Ok, then just remove the following if condition from my code and print the $line1

        #!/usr/bin/perl use strict; use warnings; open(FH, "<","./file1.txt") or die "Can't Open File1\n"; open(BH, "<","./file2.txt") or die "Can't Open File2\n"; my $pattern='^LOC_Os0[1-7]g[0-9]*.[0-9]\s'; while(my $line1 = <FH>) { chomp($line1); #print "Line1:$line1\n"; if( $line1=~ /$pattern/) { while(my $line2 = <BH>) { chomp($line2); if($line2 =~ /$pattern/) { print "Matches: ". $line1; } else { print "nothing to print\n"; } } seek(BH,0,0); } } close(FH); close(BH);


        All is well