469,923 Members | 1,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Comparing values in 2 textfiles and returning the missing values

Hi,
im trying to write a small progam to compare data in 2 textfiles.

I want to search for values that doesnt exist in File2.
The result should be "3" in the example below but Im not
able to do this since my program crosschecks all numbers in
both files and Im getting a lot of "hits". (outer and inner while-loops)
Below is an examples of the textfiles:

File1.txt File2.txt
1 1
2 2
3 4
4 5
5 6
7
8
9
10

Thanks in advance...!
Jorgen
Jul 19 '05 #1
4 4573
In article <q2********************************@4ax.com>, Jorgen
Gustafsson <jo******************@ericsson.com> wrote:
Hi,
im trying to write a small progam to compare data in 2 textfiles.

I want to search for values that doesnt exist in File2.
The result should be "3" in the example below but Im not
able to do this since my program crosschecks all numbers in
both files and Im getting a lot of "hits". (outer and inner while-loops)
Below is an examples of the textfiles:

File1.txt File2.txt
1 1
2 2
3 4
4 5
5 6
7
8
9
10

Thanks in advance...!
Jorgen


If you have a program written, it is best to include that in your post.

If your files are short enough, you can try reading file 2 first and
creating a hash with the values from file 2 as keys (the value of the
hash doesn't matter, so set it to 1 or increment to get a count). Then,
read file 1 and see if the corresponding keys exist in your hash.

Here's a quick sample program (with two files appended and separated by
a BREAK line):

#!/opt/perl/bin/perl

use strict;
use warnings;

my %seen;
while(<DATA>) {
chomp;
last if /BREAK/;
$seen{$_}++;
}

while(<DATA>) {
chomp;
if( ! $seen{$_} ) {
print "$_ not in file 1\n";
}
}

__DATA__
1
2
4
5
6
7
8
9
10
BREAK
1
2
3
4
5

__OUTPUT__
3 not in file 1

FYI: This newsgroup is defunct. Try comp.lang.perl.misc in the future
for better response.
Jul 19 '05 #2
Jorgen Gustafsson wrote:
Hi,
im trying to write a small progam to compare data in 2 textfiles.
I want to search for values that doesnt exist in File2.


perldoc -q difference:
"How do I compute the difference of two arrays? How do I compute the
intersection of two arrays?"

While the solution is written for arrays, it is trivial to modify it for
files.

jue
Jul 19 '05 #3
Jorgen Gustafsson <jo******************@ericsson.com> wrote in
news:q2********************************@4ax.com:
Hi,
im trying to write a small progam to compare data in 2 textfiles.

I want to search for values that doesnt exist in File2.
The result should be "3" in the example below but Im not
able to do this since my program crosschecks all numbers in
both files and Im getting a lot of "hits". (outer and inner while-loops)


If you're on a unix-like system, you can use the 'comm' utility for this.

--
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
Jul 19 '05 #4
Hi, thanks for all help!

Found a link to a perl-module that seems to do the job for me in comp.lang.perl.misc.
(diff and sdiff)
http://search.cpan.org/~nedkonz/Algo...m/Diff.pm#diff

/Jorgen
On Thu, 11 Dec 2003 16:58:04 +0100, Jorgen Gustafsson <jo******************@ericsson.com> wrote:
Hi,
im trying to write a small progam to compare data in 2 textfiles.

I want to search for values that doesnt exist in File2.
The result should be "3" in the example below but Im not
able to do this since my program crosschecks all numbers in
both files and Im getting a lot of "hits". (outer and inner while-loops)
Below is an examples of the textfiles:

File1.txt File2.txt
1 1
2 2
3 4
4 5
5 6
7
8
9
10

Thanks in advance...!
Jorgen


Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Chris Brew | last post: by
6 posts views Thread by sridhar | last post: by
89 posts views Thread by purifier | last post: by
88 posts views Thread by William Krick | last post: by
19 posts views Thread by Dennis | last post: by
12 posts views Thread by barcaroller | last post: by
20 posts views Thread by Bill Pursell | last post: by
3 posts views Thread by raphael.marvie | last post: by
5 posts views Thread by saneman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.