Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-01-2006, 08:04 AM   #1
Registered: Jul 2005
Location: Leeds, UK
Distribution: SuSE 10.0
Posts: 108

Rep: Reputation: 15
Perl - File Comparison

I know there's the File::Compare module which allows you to check whether two files are identical, but is there a way to check whether one file _contains_ another one?

i.e. If one file contains an entire book but another file contains just the first chapter, the second file 'matches' part of the first.
Old 09-01-2006, 01:58 PM   #2
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
I'm a Perl noob, relatively speaking. I'm not familiar with the File::Compare module of which you speak, but I would solve the problem thusly:

1. Open both files.
2. Read in the first line of the inner file (the Chapter).
3. Scan through each line of the outer file (the Book) looking for the first line.
4. If you find a matching line, read each subsequent line in both files, for as long as they match.
5. If every line in the Chapter file is found in the Book file, you have your match.
6. Otherwise, if you hit a line in the Book file that does not match the corresponding line in the Chapter file, you don't have a match, so continue scanning the Book file for that first line of the Chapter file.
7. Go back to step 4 until there is you've matched the whole Chapter or you've reached EOF of the Book.

Hope this helps. . .


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl String comparison Xris718 Linux - General 5 04-01-2005 02:59 PM
perl string comparison problem AM1SHFURN1TURE Programming 3 03-06-2005 11:29 AM
File Modification Date Comparison MadTurki Programming 7 03-08-2004 05:02 PM
DIFF (or other file comparison) ciscokid1967 Linux - General 1 12-11-2003 03:13 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration