in reply to Beginner Hash Element Comparison

It can be as simple as this:
use Modern::Perl; use IO::All -strict; use List::MoreUtils qw /each_array/; my @first_array = io('./first.txt')->chomp->slurp; my @second_array= io('./second.txt')->chomp->slurp; my $ea = each_array( @first_array, @second_array ); while ( my ( $first, $second ) = $ea->() ) { if ( $first ne $second ) { say 'Arrays differ at line ', $ea->('index'); say "First: $first"; say "Second: $second"; last; } }
It is simple and fast but not memory efficient as it reads in the whole files before starting to compare them.

This one is more memory efficient, but might be slower (depending on the length of the files and where the first difference is found):

use Modern::Perl; use IO::All; my $first_file = io('./first.txt')->chomp or die $!; my $second_file = io('./second.txt')->chomp or die $!; my $index = 0; while ( my $first_line = $first_file->getline ) { my $second_line = $second_file->getline; if ( $first_line ne $second_line ) { say "Files differ at line $index"; say "First: $first_line"; say "Second: $second_line"; last; } $index++; }

Update 1: added a more memory efficient version.

Update 2: used the slurp method in the first version.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Beginner Hash Element Comparison
by nicol004@uwp.edu (Initiate) on Feb 28, 2011 at 16:41 UTC
    Sir, I imagine that you are busy so I'll try to keep this short.
    I spent the better part of yesterday finding understanding and downloading
    those two modules in the second code you posted (Io::All and Modern::Perl)
    After some Nmake problems and all, I was able to run the thing.
    There appears to be a problem with the "io" file opening portion.
    I then wrote a bit of code to more explicitly open the files, and checked to see if it would print their contents
    it does that but when I do it that way i get no output in the command line.
    I am just wondering if you knew what might be going on there?
    I very much like the second code you posted it's very simple.
    I just want to get it working now.
    Thank you for your kind replies.

      "There appears to be a problem with the "io" file opening portion. I then wrote a bit of code to more explicitly open the files, and checked to see if it would print their contents it does that but when I do it that way i get no output in the command line. I am just wondering if you knew what might be going on there?

      You may want to edit your post and include the code you're describing above. Although CountZero helped with a solution he may not be the person who continues to help with your problem. (What if he doesn't check this site for one or two weeks?) If you show the code you wrote after trying his solution another smart member can come along, see both CountZero's work and yours, and be in a much better position to help. :-)


      "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote
      There appears to be a problem with the "io" file opening portion.
      What error messages did you get?

      I then wrote a bit of code to more explicitly open the files, and checked to see if it would print their contents. It does that but when I do it that way i get no output in the command line.
      Does it or doesn't it print out anything? Perhaps you can try adding (in the first script) a say "@first_array"; say "@second_array"; just before the loop so you are sure the arrays indeed contain the correct data.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James