Wagner, David --- Senior Programmer Analyst --- CFS wrote:
-----Original Message-----
From: Anirban Adhikary [mailto:anirban.adhik...@gmail.com]
Sent: Monday, May 04, 2009 06:40
To: beginners@perl.org
Subject: Perl code for comparing two files
Hi List
I am writing a perl code which will takes 2 more files as
argument. Then It
will check the the values of each line of a file with respect
with another
file. If some value matches then it will write the value
along with line
number to another ( say outputfile) file.
The source files are as follow
Contents of abc.txt
1 2325278241,P0
2 2296250723,MH
3 2296250724,MH
4 2325277178,P0
5 7067023316,WL
6 7067023329,WL
7 2296250759,MH
8 7067023453,WL
9 7067023455,WL
10 5000055413,EA05
#######################################################
Contents of xyz.txt
1 7067023453,WL
2 31-DEC-27,2O,7038590671
3 31-DEC-27,2O,7038596464
4 31-DEC-27,2O,7038596482
5 2296250724,MH
6 31-DEC-27,2O,7038597632
7 31-DEC-27,2O,7038589511
8 31-DEC-11,2O,7038590671
9 7067023455,WL
10 31-DEC-27,2O,7038555744
###############################################################
Contents of pqr.txt
1 2325278241,P0
2 7067023316,WL
3 7067023455,WL
4 2296250724,MH
Here is a way where a 'seen' hash has two array elements: [0] -
count, [1]: file number and line number for each seen item.
Code starts on next line:
use strict;
use warnings;
use Data::Dumper;
my %seen;
my $MyLineNbr = 1;
Why? Perl already has a built-in line number variable.
my %MFN = ();
my $MyFilenames = \%MFN;
Why declare two variables when you are only using one? Why use a hash
reference instead of just using a hash (like you do with %seen?)
my $MyFileCnt = 1;
my $MyCurrFile = q[];
my $MyActIdx = 1;
This value never changes so why use a variable? Why no variable for
index 0 of the array?
while ( <> ) {
if ( $ARGV ne $MyCurrFile ) {
printf "Filename: %s\n", $ARGV;
Why not just:
print "Filename: $ARGV\n";
$MyCurrFile = $ARGV;
$MyFilenames->{$MyCurrFile} = $MyFileCnt++;
Why not just:
$MyFilenames->{$MyCurrFile}++;
$MyLineNbr = 0;
}
chomp;
$MyLineNbr++;
next if ( /^\s*$/ );
my @elems = split (/ /, $_);
my $value = $elems[1];
Why not just:
my $value = ( split )[ 1 ];
$seen{$value}[0]++;
Why does this array element use a literal number and the next line use a
variable?
$seen{$value}[$MyActIdx] .= $MyFilenames->{$MyCurrFile} . q[;] .
$MyLineNbr. q[^];
Since you are using a HoA anyway just push()
"$MyFilenames->{$MyCurrFile};$MyLineNbr" onto the array and the count
will be the array in scalar context.
}
print Dumper(\%seen);
^--- code ends here
I leave to you to get the output, but this should give you what
need to work with.
If you have any questions and/or problems, please let me know.
Thanks.
200 lines trimmed.
Please trim your posts and remove extraneous junk at the end.
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/