Comparing two files
am 08.04.2008 00:07:34 von 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 ()
{
chomp;
my ($id2, $place) = (split(/\t/, $_))[0,1];
}
while ()
{
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
Re: Comparing two files
am 08.04.2008 00:34:38 von glex_no-spam
clearguy02@yahoo.com wrote:
> 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 ()
> {
> chomp;
> my ($id2, $place) = (split(/\t/, $_))[0,1];
> }
>
> while ()
> {
> 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