in reply to Compare 2 files and get data

open(file1,"file1.txt");
open my $file1, '<', "file1.txt" or die "Can't open `file1.txt': $!\n";
while (<file1>) { chop();
chomp.
$REC = $_; @LINEREC = split(/\,/,$REC);
(Do a big favour to yourself and)
use strict; use warnings;
at the top of your program. Then the above would have to become
my $REC = $_; my @LINEREC = split(/\,/,$REC);
It's not that much more typing and it won't hurt your fingers. OTOH it will help you immensely to avoid common mistakes...
open(file2,"file2.txt"); while (<file1>)
Huh?!? Did you paste your script or did you retype it? Hint: the former is a much more reliable option...
if ( $LINEREC[0] eq $LINEREC[1]) { print $LINEREC[0]; {
Ditto as above wrt retyping. It is common sense to post code that at least compiles.
i have tried the following but doesn't work:
Indeed. How 'bout (something along the lines of:)
#!/usr/bin/perl use strict; use warnings; die "Usage: $0 <file1> <file2>\n" unless @ARGV == 2; my ($file2, %have)=pop; while (<>) { chomp; $have{ (split /,/)[0] }=1; } @ARGV=$file2; my %saw; $\="\n"; while (<>) { chomp; local $_=(split /,/)[1]; next if $saw{$_}++; print if $have{$_}; } __END__
Note: this is meant as being a minimal example. If your actual code is more complex of course you'd better open your filehandles explicitly. I also made some assumtpions: for example that you don't want to output duplicate entries or that the format of 'file1' is different from that of 'file2' and that both are fixed and corresponding to the one that one could infer from your examples.