Click here to get back home

Comparing two files

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Comparing two files clearguy02 04-07-2008
Posted by clearguy02 on April 7, 2008, 6:07 pm
Please log in for more thread options

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

Posted by J. Gleixner on April 7, 2008, 6:34 pm
Please log in for more thread options
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 (<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

Similar ThreadsPosted
Comparing XML files August 8, 2005, 6:22 pm
Comparing two files January 15, 2008, 3:55 pm
Comparing huge XML Files February 23, 2005, 1:14 am
Comparing 2 XML files need some suggestions please April 15, 2005, 7:14 am
comparing modification times of files September 13, 2006, 11:10 am
Diff files with regex comparing May 13, 2007, 11:40 am
Parsing two files and comparing the first fields.. November 28, 2007, 6:12 pm
Comparing 2 dates ? December 20, 2004, 9:41 am
comparing two numbers March 4, 2005, 1:37 pm
comparing two dates July 29, 2005, 2:02 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap