Comparing two files

C

clearguy02

Hi folks,

I have the following piece of code to compare two files: The first one
has list of all id's. The second has a list of both id's and places. I
need to compare the id's in the both lists. If the id in the first
file matches with the id in the second file, then the id in the first
file should be concatenated with the repspective place field (from the
second file) with a tab.

Here is the code:
++++++++++++++++++
open (IN1, "id1.txt") || die "Can not open the file: $!";
open (IN2, "id2_place.txt") || die "Can not open the file: $!";

while (<IN2>)
{
chomp;
my ($id2, $place) = (split(/\t/, $_))[0,1];
}

while (<IN1>)
{
chomp;
print "$_\t$place\n"
}
+++++++++++++++++++

I know there is some thing wrong in storing the $place value in the
first while loop.. what am I doing wrong here?

Thanks in advance,
J
 
J

J. Gleixner

Hi folks,

I have the following piece of code to compare two files: The first one
has list of all id's. The second has a list of both id's and places. I
need to compare the id's in the both lists. If the id in the first
file matches with the id in the second file, then the id in the first
file should be concatenated with the repspective place field (from the
second file) with a tab.

Here is the code:
++++++++++++++++++
open (IN1, "id1.txt") || die "Can not open the file: $!";
open (IN2, "id2_place.txt") || die "Can not open the file: $!";

Should use the 3 argument open and it's good to include the
name of the file in the error message.

See: perldoc -f open

while (<IN2>)
{
chomp;
my ($id2, $place) = (split(/\t/, $_))[0,1];
}

while (<IN1>)
{
chomp;
print "$_\t$place\n"
}
+++++++++++++++++++

I know there is some thing wrong in storing the $place value in the
first while loop.. what am I doing wrong here?

You need to store $place so that you can find it by a key like $id2.
If $id2 is always unique, you could use a hash.

use strict;
use warnings;

my %ids;
open (my $id1, '<', 'id1.txt' )
|| die "Can not open id1.ext: $!";
open (my $id2, '<', 'id2_place.txt' )
|| die "Can not open id2_place_txt: $!";
while( <$id2> )
{
chomp;
my ( $id2, $place ) = split(/\t/, $_, 2);
$ids{ $id2 } = $place;
}
close( $id2 );

Now you have a hash, %ids, that has the values of $id2 as its
key and $ids{ 'someid' } holds the corresponding value
for $place, found in your file.

Now you can iterate through your id1.txt file, printing the
line and the $place value found in id2_place.txt, only if
that key was found in id2_place.txt.

while ( <$id1> )
{
chomp;
print "$_\t$ids{ $_ }\n" if exists $ids{ $_ };
}
close( $id1 );


If your $id2 values are not unique, you'll need to use some
other data structure. See: perldoc perldsc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top