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.

In reply to Re: Compare 2 files and get data by blazar
in thread Compare 2 files and get data by darrengan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.