
Karazam
User
Mar 26, 2011, 9:23 AM
Post #13 of 17
(5920 views)
|
Re: [perlfree] Search content of one file with the content of a second file
[In reply to]
|
Can't Post
|
|
while (my $line = <RF>){ $line = split('\t'); $var1{$1} = {2}; } There are three errors here. First, "$line = split('\t');" won't mean anything in this context. $line is one line of your input file, it already has a value, and "split" is not given any value to work with. Second, if you want insert a value into a hash, the syntax would be "$var1{$1} = 2;". Third, the scalar $1 has not been defined anywhere. What is it you want this piece of code to do exactly?
open(DATA,"+>OutFile.txt") or die "Can't open data"; Don't use '+>', it is almost always wrong. This is the way:
open(DATA, '>', 'OutFile.txt') or die "Can't open data"; Or even better, use lexical filehandles (that goes for your previous open's too):
open my $data_fh, '>', 'OutFile.txt' or die "Can't open data"; (And use 'or', not '||'.)
if (exists $var1{$var2}){ ... The $var2 in this situation has nothing whatsoever to do with the hash %var2, but is a wholly separate scalar value which you haven't defined anywhere. Therefore you get the error. Now, if I understand you original post correctly, you want too check if any of the lines in File2 also occurs in File1. So, read in one of the files into a hash (lets take the smallest file to conserve memory):
my %seen; while (my $line = <RXNs>) { chomp $line; $seen{$line} = 1; } Then just read the other file in a loop and check the %seen hash:
while (my $line = <RF>) { chomp $line; if ( exists $seen{$line} ) { print "Found it! $line\n"; } } Maybe there's some reason why you try to use "split", but as your original question was phrased it seems unnecessary. Hope this helps.
|