PDA

View Full Version : Comparing 2 files


Anthony_V
08-07-03, 14:10
Hello every, I have two files that I want to compare some values between the two.

File A

rms_r4_4 3.8336e-05
rms_r4_43 7.526e-05
avg_r4_44 6.2331e-06
avg_r4_50 1.4547e-05
avg_r4_11 3.242e-10
.....
....
.....

File B

avg_r4_31 0.0032727
rms_r4_31 0.0155373
avg_r4_5
rms_r4_5 0.0405308
avg_r4_12 7.71647e-16
......
.....
.....

I like to take the name in file A and matched to file B then print out it's values.

Here is the partial code:

#!/usr/intel/bin/perl

use strict;

my (@names, @matches);

open my $A, "/home/avo/rv_validation/com_ouput" || die "file not found\n";

while (my $line = <$A>) {
my ($name, $current ) = split(/\s+/), $line, 2;
push (@names, $name);
}
close ($A);

open my $B, "/home/avo/rv_validation/net_output" || die "file not found\n";

while (my $line = <$B>) {
my ($name, $current1 ) = split(/\s+/), $line, 2;
push (@matches, $name ) if (grep /$name/, @names);
}
close ($B);

print "$_\n" for (@matches);

The error message I got when running this script are:
Global symbol "line" requires explicit package name at comp_all.pl line 10.
syntax error at comp_all.pl line 23, near ""$_\n" for "
Execution of comp_all.pl aborted due to compilation errors.

Can someone help me with this? thanks for the help in advance.

Anthony

WebRecka
08-11-03, 16:51
Ide write the script a bit differently...

#!/usr/bin/perl

use strict;

open(file,"filea.txt") || die ("Couln't open file! $!"); #change filename
my @names=<file>;
close(file);

open(file,"fileb.txt") || die ("Couln't open file! $!"); #change filename
my $count=0;
my @matches;
while(<file>){
chomp($_);
chomp($names[$count]);
if(lc($_) eq lc($names[$count])){ #i like to do lc() when comparing strings
push(@matches,$_);
}
$count++;
}
foreach(@matches){
print $_."<Br>";
}



BTW your perl syntax is EXTREMLY ODD! Keep to traditional and use () as well. It makes it much easier to understand. For something like split traditional is:
@array=split(/regx/,$string);
makes life much easier, and fileopens:
open(filehandle,"[>|>>]Filename");
Etc. G/l with the script