CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
compare files

 



abhi
Novice

Feb 26, 2013, 4:29 AM

Post #1 of 6 (995 views)
compare files Can't Post

Hi
I need to compare two files.
One is primary file and other is secondary.
I need to check if secondary file contains each line of primary (may be in different order) and should not contain any extra data.

exmaple:
cat primary
abc
lmn
xyz

cat secondary:
cat secondary
xyz
abc
lmn

then in this case compare function should give true.

Note: file contains may be any thing like html or xml code or other.


FishMonger
Veteran / Moderator

Feb 26, 2013, 6:09 AM

Post #2 of 6 (986 views)
Re: [abhi] compare files [In reply to] Can't Post

What have you tried thus far to complete your homework assignment?


abhi
Novice

Feb 26, 2013, 6:35 AM

Post #3 of 6 (983 views)
Re: [FishMonger] compare files [In reply to] Can't Post

nop its a interview question asked to me but i couldn't ans so now i am trying to get ans


BillKSmith
Veteran

Feb 26, 2013, 9:36 AM

Post #4 of 6 (975 views)
Re: [abhi] compare files [In reply to] Can't Post

Check the FAQ.

perldoc -q "How do I test whether two arrays or hashes are equal"
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Feb 26, 2013, 10:41 AM

Post #5 of 6 (971 views)
Re: [abhi] compare files [In reply to] Can't Post

The canonical way was to load one file in a hash (with each line being the key of the hash) and check the other file against the hash. Since you want to compare both ways, you'll need to flag in the hash the entries found when reading the second file to verify that all entries have been visited.

Starting with Perl version 5.10, there are other ways requiring less work (using the smart match operator), but both files have to be loaded into an hash (or an array).

If you are using an older version, there are some CPAN modules that can do a good part of the work for you.


Kenosis
User

Feb 26, 2013, 11:37 AM

Post #6 of 6 (968 views)
Re: [abhi] compare files [In reply to] Can't Post

Building upon the wisdom of BillKSmith and Laurent_R, consider the following which uses File::Slurp (only to easily handle the file i/o) and Test::More (to do a 'deep' comparison of the two hash structures built from the two files):

Code
use strict; 
use warnings;
use File::Slurp qw/read_file/;
use Test::More tests => 1;

my ( %file1, %file2 );

do { chomp; $file1{$_}++ } for read_file 'file1.txt';
do { chomp; $file2{$_}++ } for read_file 'file2.txt';

is_deeply( \%file1, \%file2 );

Output if same:

Code
1..1 
ok 1

Output if different:

Code
1..1 
not ok 1
# Failed test at ... line 11.
# Structures begin differing at:
# $got->{123} = '1'
# $expected->{123} = Does not exist
# Looks like you failed 1 test of 1.

Why use a hash? In this case, the hashes track the number of times the content of lines (as keys) appears. For example, if "abc" occurs in file1.txt only once, then the value of $file1{abc} will be 1, and the value will be 2 if "abc" occurs twice...


(This post was edited by Kenosis on Feb 26, 2013, 11:39 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives