Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
If the files are not huge (i.e., one of them will fit in memory), I would go with the approach specified by hippo. Read file #1 into hash, then compare each line in file #2 to the hash.

If the files are too big for this, a slight modification: Read file #1 and store the seek (or tell) locations in the hash. Then compare each line in file #2 to the corresponding line in file #1, using your hash as a shortcut way to go straight to that line.

Update: Sample of first option:

#!/usr/bin/perl use strict; use warnings; my %FileInfo1 = (); my ($inputFilename1, $inputFilename2, @otherParameters) = @ARGV; # Read File 1 to Hash open INPUT_FILE1, '<', $inputFilename1; while (my $inputBuffer1 = <INPUT_FILE1>) { chomp $inputBuffer1; my ($key, $data) = split /\|/, $inputBuffer1, 2; $FileInfo1{$key} = $data; } close INPUT_FILE1; # Read File 2 and Compare open INPUT_FILE2, '<', $inputFilename2; while (my $inputBuffer2 = <INPUT_FILE2>) { chomp $inputBuffer2; my ($key, $data) = split /\|/, $inputBuffer2, 2; if (!defined $FileInfo1{$key}) { print "$key not found in $inputFilename1\n"; } elsif ($FileInfo1{$key} ne $data) { print "$key data does not match\n"; delete $FileInfo1{$key}; } else { print "$key - OK\n"; delete $FileInfo1{$key}; } } close INPUT_FILE2; foreach my $leftoverKey (keys %FileInfo1) { print "$leftoverKey not found in $inputFilename2\n"; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-10-08@1245-TwoFile-Keyed-Compare>type te +st*.dat test1.dat A001|Steve|45 A002|George|32 A003|Alice|24 test2.dat A001|Steve|45 A003|Alice|23 A004|Mike|48 C:\Steve\Dev\PerlMonks\P-2013-10-08@1245-TwoFile-Keyed-Compare>perl cm +pfiles.pl test1.dat test2.dat A001 - OK A003 data does not match A004 not found in test1.dat A002 not found in test2.dat

In reply to Re: Comparing strings from different files by marinersk
in thread Comparing strings from different files by Jalcock501

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2015-07-05 08:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (61 votes), past polls