CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2006
    Posts
    50

    [RESOLVED] Perl: compare two files & output to a new file

    Compare two files and output the difference in a new file?

    nameList.txt
    Code:
    | | |AAAAAAA*                 | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
    
    | | |AAAAAAA*                 | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
    
    | | |CCCCCCC*                 | |*         | |9999| |NAD         | |-           | |XXXXXXXXX| | |
    
    | | |CCCCCC   D*              | |*         | |9999| |BEM         | |-           | |XXXXXXXXX| |YYYYY|
    
    | | |XXXXXX   A*              | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
    
    | | |ZZZZZZ   A*              | |*         | |9999| |NAD         | |-           | |XXXXXXXXX| | |
    
    | | |EEASAW   A*              | |*         | |9999| |NA*         | |-           | |XXXXXXXXX| |YYYYY|
    
    | | |ASCAWF W A*              | |*         | |9999| |ME*         | |*           | |XXXXXXXXX| | |
    
    | | |XXXXXX A A*              | |*         | |9999| |BE*         | |*           | |XXXXXXXXX| | |
    
    | | |AWSDAW*                  | |*         | |9999| |ME*         | |-           | |XXXXXXXXX| | |
    
    | | |WFCAPI   A2*             | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
    checkList.txt
    Code:
    1 XXXXXXX 6 6 U 1 2 3 4 5 6 1 1 P 1 0 1 0 
    2 AAAAAAA -12 11 Y 1 2 3 4 5 6  469.7 481.7 P 1 0 1 0 
    3 FASZFAS -12 -6 Z 1 2 3 4 426.4 431.7 Z 0 0 1 0 
    4 JJHJHGC -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0 
    5 JHGJHZA -9 -4 Z 1 2 3 4 405.6 410.7 Z 0 0 1 0 
    6 7843JHE -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0 
    7 NMHZJYA -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0 
    8 WFCAPI -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0 
    9 FASTYTA -8 8 Y 1 2 3 4 5 6  446.5 457.6 P 1 0 1 0 
    10 89QANJGA -14 -7 Z 1 2 3 405.6 410.7 Z 0 0 1 0
    I was wondering how I can do this by passing nameList.txt in array, probably with AAAAAAA or ASCAWF, begin third characters.

    Then grab the second file, checkList.txt, on text such as FASZFAS or NAETGNA, second column.

    Then if array in checkList.txt doesn't match with nameList.txt, output that name to a new file.
    This will keep on doing until the end of file and keep on appending the name to the same new file.

    Functions I probably need to use is:
    open()
    close()
    while loop
    compare method

    Can any one help me started?

  2. #2

    Re: Perl: compare two files & output to a new file

    I'm not entirely sure what you're trying to do here....

    If you want to actually compare the contents of files, save yourself some trouble and use CPAN modules...

    http://perl.active-venture.com/lib/File/Compare.html

    If you need to compare contents of the files, basically load each file into an array and loop over the entries accordingly.

  3. #3
    Join Date
    Feb 2006
    Posts
    50

    Re: Perl: compare two files & output to a new file

    The Cpan compare the whole files though.

    I want to compare if the text match or not.
    Like the nameList.txt is after 3 character until it hit whitespace or tab.

    Just grab this only: | | |CCCCCCC* and takes only CCCCCCC and ignore the rest on that line.

    And in checklist.txt, just grab the second column only.
    1 CCCCCCC takes only --> CCCCCCC

    When finised grabbing both files, compare those two.

    If column grab in checkList.txt doesn't match with name grab in nameList.txt, output that name to a new file.

  4. #4

    Re: Perl: compare two files & output to a new file

    Ok, basically, here's the commands you need to know...

    To read a file into an array (one of a bazillion ways to do this):

    Code:
    my $file1 = $ARGV[0];
    open(FILE1, $file1) || die "Can't open $file1: $!\n";
    my @file1data = <FILE1>;
    close(FILE1);
    To read each entry in an array and split the values according to a character:

    Code:
    foreach my $line (@file1data)
    {
       my @linedata = split(/|/, $line);
       # Do something with the @array...
       # Note that you can also do something like
       # my ($col1, $col2, ... $coln) = split(/|/,$line)
       # to get the data in named variables
    }
    My final hint is remember that you must use "eq" in perl to check for equality in string scalars. Using "==" will give you a positive result more than it should....

  5. #5
    Join Date
    Feb 2006
    Posts
    50

    Re: Perl: compare two files & output to a new file

    Code:
    #!/usr/local/bin/perl
    
    open(FILE0, $ARGV[0]) || die "Can't open $_: $!\n";
    open(FILE1, $ARGV[1]) || die "Can't open $_: $!\n";
    @file0data = <FILE0>;
    @file1data = <FILE1>;
    close(FILE0);
    close(FILE1);
    
    @linedata=();
    @line1data=();
    
    
    while(@file0data)
    {
       @linedata = split(/|/);
    
    }
    
    while(@file1data)
    {
       @line1data = split(/\t/);
    }
    
    while(@file1data)
    {
       @line1data = split(/\t/);
    
    }
    
    print("FILE: $linedata[3] \n\n");
    print("FILE2: $line1data[1] \n\n");
    
    
    ##check
    
    while(@line1data)
    {
            if($line1data[1] eq $linedata[3])
            {
            #output $line1data[1] & append to fileCheck.txt
            }
    
    }
    Here the code I try, it does pass the text in the file into the array, but the split doesn't work.

  6. #6

    Re: Perl: compare two files & output to a new file

    Save yourself some trouble - be explicit with your variables. I've found some odd behavior when relying on $_, especially if use strict is not defined.

    Also, what do you mean by the split doesn't work - based on your entry, you're only going to see the last piece of data. The split I illustrated works on a per line basis. Each iteration through the loop overwrites @linedata.
    Last edited by mmetzger; March 2nd, 2006 at 03:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured