Comparing two files

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Apr 7, 2008.

  1. Guest

    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
    , Apr 7, 2008
    #1
    1. Advertising

  2. J. Gleixner Guest

    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
    J. Gleixner, Apr 7, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GenxLogic
    Replies:
    3
    Views:
    1,241
    andrewmcdonagh
    Dec 6, 2006
  2. Frost
    Replies:
    8
    Views:
    501
    Vladimir S. Oka
    Feb 10, 2006
  3. Nick Matzke

    Comparing two book chapters (text files)

    Nick Matzke, Feb 5, 2009, in forum: Python
    Replies:
    1
    Views:
    272
    andrew cooke
    Feb 5, 2009
  4. ruds

    comparing two test files

    ruds, Dec 23, 2011, in forum: Java
    Replies:
    5
    Views:
    372
    Gene Wirchenko
    Dec 23, 2011
  5. Edgardo Hames

    Comparing two files for equality

    Edgardo Hames, Jan 12, 2005, in forum: Ruby
    Replies:
    11
    Views:
    454
    Martin DeMello
    Jan 18, 2005
Loading...

Share This Page