Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Compare 2 files and get data

by jpeg (Chaplain)
on Sep 05, 2005 at 08:49 UTC ( [id://489170]=note: print w/replies, xml ) Need Help??


in reply to Compare 2 files and get data

yes, the logic is at fault. The while() loops are executed sequentially and the if block is evaluated after both have completed, so the script is comparing elements from the last line of each file. You'll need to nest those while loops or better yet, read the elements into arrays and compare them.

I don't mean to cramp your style, but there are a few points I feel I should mention.

  • you'll learn a lot from
    use strict; use warnings;
    at the beginning of each of your scripts.
  • chomp (perldoc -f chomp) is a bit safer to use for stripping newlines from a file you've read. chop is guaranteed to strip the last character, which theoretically could be only one part of a system's newline convention.
  • Again, not a huge deal, but most people use all caps for constants (e.g., open(FILE, $file) and avoid all caps for variables. Not a big deal unless you're sharing code with someone else.
  • It's a good idea to close() filehandles that you open().
  • It's a good idea to check for errors when you interact with your system (like when you're opening files). Try using  open(FILE, $file) or die "open $file failed due to $!" in a script and open(FILE, $file) in another, and use them with a nonexistant file.
Again, I don't mean to cramp your style or make you uncomfortable, but these tips help a lot of people.
--
jpg

Log In?
Username:
Password:

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

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

    No recent polls found