Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Comparing specific columns from 2 files

by 1nickt (Canon)
on Jul 29, 2015 at 15:33 UTC ( [id://1136759]=note: print w/replies, xml ) Need Help??


in reply to Comparing specific columns from 2 files

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

use strict; use warnings;

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136759]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-16 17:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found