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:
A 1_1
A 1_2
B 1_3
C 1_4
and FILE2 is:
A 2_1
B 2_2
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 matchThe 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.
#!perl open (FILE1, $ARGV[0]); open (FILE2, $ARGV[1]); while ($_ = <FILE1>) { chomp; @FILE1 = split; ($FILE1letter, $FILE1number) = @FILE1; @FILE2 = split(' ',<FILE2>); ($FILE2letter, $FILE2number) = @FILE2; # print "$FILE1letter from FILE1 with number $FILE1number and $FILE +2letter from FILE2 with $FILE2number match\n"; #prints the same as below if ($FILE1letter == $FILE2letter) { print "$FILE1letter from FILE1 with number $FILE1number and $F +ILE2letter from FILE2 with number $FILE2number match\n"; } else { print "$FILE1letter from FILE1 with number $FILE1number and $F +ILE2letter from FILE2 with number $FILE2number DO NOT match\n"; }
Output from above code:
A from FILE1 with number 1_1 and A from FILE2 with number 2_1 match
B from FILE1 with number 1_2 and C from FILE2 with number 2_2 match
C from FILE1 with number 1_3 and from FILE2 with number match
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple comparison of 2 files
by AnomalousMonk (Archbishop) on Jul 27, 2016 at 23:00 UTC | |
|
Re: Simple comparison of 2 files
by haukex (Archbishop) on Jul 28, 2016 at 10:38 UTC | |
|
Re: Simple comparison of 2 files
by Marshall (Canon) on Jul 27, 2016 at 19:05 UTC | |
by Q.and (Novice) on Jul 27, 2016 at 19:12 UTC | |
by Paladin (Vicar) on Jul 27, 2016 at 19:24 UTC | |
by pryrt (Abbot) on Jul 27, 2016 at 19:30 UTC | |
by Q.and (Novice) on Jul 27, 2016 at 19:38 UTC | |
| |
by Q.and (Novice) on Jul 27, 2016 at 19:32 UTC | |
by Marshall (Canon) on Jul 27, 2016 at 19:47 UTC | |
|
Re: Simple comparison of 2 files
by neilwatson (Priest) on Jul 27, 2016 at 19:02 UTC | |
by Q.and (Novice) on Jul 27, 2016 at 19:16 UTC | |
by Anonymous Monk on Jul 27, 2016 at 20:43 UTC | |
|
Re: Simple comparison of 2 files
by Anonymous Monk on Jul 27, 2016 at 20:29 UTC |