473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 4708
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 ascending order of key. I read all the streams...
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 unsigned int is 32.The compiler gives a warning
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 statements if u want it to be a little more...
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 implementations below seem to work fine but I'm...
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 has specified a color like; if mycolor =...
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 topical... I have two pointers, the first of which is...
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, I have tried a simple test which result surprises...
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.