472,342 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 4652
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris Brew | last post by:
I've just been writing some code to merge items from a collection of streams, where each item has a key, and each stream is known to be sorted in...
6
by: sridhar | last post by:
#include <stdio.h> int main(){ unsigned int ui = 0; if(0x0ul <= ui){ printf("less eq\n"); } } On my system unsigned long is 64 bits and...
89
by: purifier | last post by:
The problem is to write a program in 'C' to find the greatest of 2 given numbers... Easy? huh here's the catch do not use 'if' or any conditional...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not...
3
by: raphael.marvie | last post by:
Dear all, I am trying to compare graphes of object through the use of the __cmp__ operator. Before managing the problem of recursive comparison,...
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.