Re: [perlfree] Search content of one file with the content of a second file
[In reply to]

Can't Post

That script won't even compile, so start with the first error and fix it then rerun the script and fix the next error in line. Keep doing that until there are no more errors. If you don't know how to fix the error, post the updated code and the exact error message.

Re: [perlfree] Search content of one file with the content of a second file
[In reply to]

Can't Post

Code

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?

Code

open(DATA,"+>OutFile.txt") or die "Can't open data";

Don't use '+>', it is almost always wrong. This is the way:

Code

open(DATA, '>', 'OutFile.txt') or die "Can't open data";

Or even better, use lexical filehandles (that goes for your previous open's too):

Code

open my $data_fh, '>', 'OutFile.txt' or die "Can't open data";

(And use 'or', not '||'.)

Code

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):

Re: [Karazam] Search content of one file with the content of a second file
[In reply to]

Can't Post

Since this is a homework assignment, I was trying to avoid giving away the solution, however since the "cat's out of the bag", I'll post one anyway, but I won't explain it. The OP should try to figure it out.

Code

#!/usr/bin/perl

use strict; use warnings;

open my $rf_fh, '<', 'file1.txt' or die "failed to open file1.txt $!"; open my $rxn_fh, '<', 'file2.txt' or die "failed to open file2.txt $!";

You don't need my help with code. However, I'd just like to point out that autovivification isn't really an issue here. Additionally, map in a void context is pretty messy in my opinion, better to use for, or even better grep. Nevertheless, you know all that stuff.

Following your example, I won't supply code of what I'm describing, just couldn't help but chime in.

To OP,

Just look at perlfaq4, search for "How can I remove duplicate elements from a list or array?", to see another example of the technique that Fishmonger is demonstrating.

Re: [miller] Search content of one file with the content of a second file
[In reply to]

Can't Post

In Reply To

Thanks everyone for suggestions and help.

I tried the code from Fishmonger - it works. However, I have to apologise for confusing evryone on the issue of File 2. File2 should actually just contain the following(one column):

MSeef F_ecjc

This now brings me to Karazam' question about what I was trying to achieve with splitting:

My approach was to split File1 with '\t' and then just copare the first part File1{1} with the contents of File 2 which should be just one column. Once again I apologise for this misake. Clearly, Fishmonger's code addresses what everyone thought I was trying to use for searching (i.e two columns in File2 - which should not be case).

Re: [miller] Search content of one file with the content of a second file
[In reply to]

Can't Post

Quote

Additionally, map in a void context is pretty messy in my opinion, better to use for, or even better grep.

Yes I agree and almost changed that line to not use map. When I first wrote it, I assigned the return to $dups which was to be used latter to show the number of matches.

Although, I recently read some threads (I think on perlmonks) that p5p was reworking the map coding to give credence to cases where it would be appropriate to use it in void context. I don't recall the details of those threads.