|more useful options
Simple comparison of 2 filesby Q.and (Novice)
|on Jul 27, 2016 at 18:47 UTC
Q.and has asked for the wisdom of the Perl Monks concerning the following question:
I know this is an elementary problem, so if this is a repeated question, please kindly flag, but I have yet to find something that works in the way I'm thinking about the problem. If the code seems overcomplicated for the task at hand, it's in part because the real script and files are more complex and because I'm new, so any suggested solutions that stay as close as possible to the code given here will be most appreciated.
Say I have two files, FILE1 contains:
and FILE2 is:
I would like to compare both files and have it print:A from FILE1 with number 1_1 and A from FILE2 with number 2_1 match
A from FILE1 with number 1_1 and B from FILE2 with number 2_2 DO NOT match
A from FILE1 with number 1_2 and A from FILE2 with number 2_1 match
A from FILE1 with number 1_2 and B from FILE2 with number 2_2 DO NOT match
B from FILE1 with number 1_3 and A from FILE2 with number 2_1 DO NOT match
B from FILE1 with number 1_3 and B from FILE2 with number 2_2 match
C from FILE1 with number 1_4 and A from FILE2 with number 2_1 DO NOT match
C from FILE1 with number 1_4 and B from FILE2 with number 2_2 DO NOT match
The way I've been constructing the code so far however, obviously does not loop over the files in the way that I'm aiming for. That code is below, along with example output.
Output from above code:
I would appreciate any direction, brief explanation of what about my current code is not feasible for producing my desired output, and/or suggestions for better constructing the script. Thanks in advance.
Clarification: The goal of the real script is really not to just print matches and mismatches, but to do many things within each loop. However, I realized that my if statement was not evaluating correctly, as it was printing even when there was no match and I think the larger problem is the looping structure of the entire code. In the real script, I would like it to evaluate lines from the two files ONLY when $FILE1letter is equal to $FILE2letter, but have it simplified to just a difference detection problem above in order to try to help myself learn how to solve smaller problems within larger ones.