The stupid question is the question not asked | |
PerlMonks |
Re: Comparing specific columns from 2 filesby 1nickt (Canon) |
on Jul 29, 2015 at 15:33 UTC ( [id://1136759]=note: print w/replies, xml ) | Need Help?? |
Hi arunsriniv. You've already been given suggestions about how to accomplish your goal. What you have is not the right way to go about it. But here are some comments on the code you posted. First, you should always place
at the top of your program. This tells Perl to point out errors in your code before it even runs. In your case you are not declaring your variables within the scope they are used in, which makes them global. This is bad practice. You correctly execute chomp() on $cur_data within the inner loop, but you don't need to chomp( $org_data ) each time; that should be in the outer loop. It's best to chomp() lines from a file as you read them. It would be better to read in the lines from the original file (and store them in a hash as others have said), and then read the current file in one line at a time, chomp()ing and comparing as you go. No need to put all the lines in an array and no need for a flag; just print an error and call last() or die() or whatever when a comparison fails.
The way forward always starts with a minimal test.
In Section
Seekers of Perl Wisdom
|
|