Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Comparing values in 2 textfiles and returning the missing values

Reply
Thread Tools

Comparing values in 2 textfiles and returning the missing values

 
 
Jorgen Gustafsson
Guest
Posts: n/a
 
      12-11-2003
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      12-11-2003
In article <(E-Mail Removed)>, Jorgen
Gustafsson <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-12-2003
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


 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-12-2003
Jorgen Gustafsson <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
 
Reply With Quote
 
Jorgen Gustafsson
Guest
Posts: n/a
 
      12-12-2003
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to search multiple textfiles ? Stef Mientki Python 12 10-01-2008 09:10 AM
comparing two lists and returning "position" hiro Python 12 06-25-2007 02:17 PM
Problem with loading textfiles into dictionaries. mercuryprey@gmail.com Python 6 01-31-2005 06:25 AM
XSLT problem comparing prior sibling, not returning correct value, returns original Phoenix XML 3 12-30-2004 09:51 PM
Beginners question - textfiles Rish ASP General 1 12-05-2003 12:00 PM



Advertisments