473,597 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4717
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.c om:
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
1806
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 once, then pick the minimum element of the results, replace it with a new one read from the corresponding stream, and repeat. Thing is, what happens at end of file? I'd like to do it by making the streams that are finished
6
8034
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
3384
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 tougher you can use the if but this time no relational operators or any of the predefined functions.... Can someone please help me solve the problem....
88
22004
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 wondering if one is better than the other or if there is some sort of hybrid of the two that would be superior. IMPLEMENTATION 1: #ifndef HAVE_STRCASECMP
19
2637
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
12
25981
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
2133
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
3
1073
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 me. Here is the simplest code I can write that presents my problem: </pre> $ cat cmp.py
5
4508
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
7965
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8271
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.