Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comparing two files

Reply
Thread Tools

Comparing two files

 
 
clearguy02@yahoo.com
Guest
Posts: n/a
 
      04-07-2008

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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      04-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing two test files ruds Java 5 12-23-2011 07:13 PM
Comparing two book chapters (text files) Nick Matzke Python 1 02-05-2009 09:42 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Comparing Two Files line by line and word by word Frost C Programming 8 02-10-2006 11:16 AM
Comparing two files for equality Edgardo Hames Ruby 11 01-18-2005 05:53 AM



Advertisments