Mariano Loza Coll wrote:
Hello everyone,
Hello,
Here's what I need to do: I need to take each of the words in one List1, search for their presence (or not) in a second List2 and make a new list with all the words in both. These are lists of gene names, which can often include numbers and symbols in addition to letters. The very newbie code that I wrote performs well, but it's missing the words that have parentheses in them (for instance "Su(var)2-5"). Below are examples of the lists that I'm working with, the code that I'm using, and the output. I realize that there may be a number of ways to do what I need to do (most of them better, I bet), and I'd love to learn about them. But now I'm mostly curious about why grep// cannot "see" words with parentheses in either (or both lists). I suspect the trick may be somehow escaping the (), but I tried a number of ways of doing that to no avail. Any help will be greatly appreciated! And as usual, if you ever have questions about molecular biology and genetics, fire away - I'd love to pay the favor back. Thanks in advance, Mariano Example of List 1 numb Dl cad99C ham esg Stat92E Hh l(2)Lg neur CG32150 sox15N Su(var)2-5 E(spl)-m4 ci brd vvl Example of List 2 Src42A cad99C ham Hh l(2)Lg neur sox15N numb ubx Su(var)2-5 esg E(spl)-m4 ci ttk egfr brd ocho vvl CG32150 ############## SCRIPT BEGINS #!usr/bin/perl use warnings;
use strict;
print "\nEnter path to List 1\n\n"; $path1 =<STDIN>; chomp $path1; print "\nEnter path to List 2\n\n"; $path2 =<STDIN>; chomp $path2; open (INPUT1, "$path1"); open (INPUT2, "$path2"); open (INPUT3, "$path3");
You should *always* verify that a file opened correctly: open INPUT1, '<', $path1 or die "Cannot open '$path1' because: $!"; open INPUT2, '<', $path2 or die "Cannot open '$path2' because: $!"; open INPUT3, '<', $path3 or die "Cannot open '$path3' because: $!";
@array1 =<INPUT1>; @array2 =<INPUT2>; my ($array1,$array2,$in1and2);
You never use those variables anywhere.
my $ctr1 = 0; while ($ctr1< @array1){ if(grep(/^$array1[$ctr1]$/,@array2)){
Change that line to this to get your program to work correclty: if ( grep $_ eq $array1[ $ctr1 ], @array2 ) {
push (@in1and2,$array1[$ctr1]); } $ctr1 +=1; } print "in 1= ".@array1."\nin 2= ".@array2."in 1 and 2= ".@in1and2."\n\nDone!"; exit; ###### The output is... in 1= 16 in 2= 19 in 1 and 2= 11 Done! ...when it should have been... in 1= 16 in 2= 19 in 1 and 2= 14 but it's not "seeing" l(2)Lg, Su(var)2-5, E(spl)-m4 in both lists... (I know this based on troubleshooting)
John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/