LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2003, 08:10 AM   #1
morbid_ru
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Rep: Reputation: 0
Need to compare 2 databases. Help please!


Here is my problem.

I need to compare to text files. Data in the file is in this format:

Item number|Quantity

Example:

File 1:

40|1
35|2
36|1

or (40|1, 35|2, 36|1)

File 2:

35|1
42|2
36|0

or (35|1, 42|2, 36|0)

What I want to do is to comapre 2 databases based on the Item number.

Te result of this comparisson should be:

40|1
35|3
36|0
42|2

or (40|1, 35|3, 36|0, 42|2)

As you see, if File 2 contain the same item, quantities should be added, if quantity in file 2 = "0", it should change to "0" in File 1.
If item that is in File 2 is not present in File 1, it should be simply added to file 1. If item exist in File 1 and does not exist in File 2, it should remain in File 1.

Anyone who knows how to do this in Perl, please help me with the code for this!

Many thanks!
Alex
 
Old 12-08-2003, 04:16 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
#!/usr/bin/perl

my %foo;
while(<>) {
        m/([0-9]*)\|([0-9]*)/;
        $foo{"$1"} += $2;
}

foreach(keys(%foo)) {
 print "Hash is : $_:$foo{$_}\n";
}
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
programming and databases msound Programming 6 05-05-2005 04:52 PM
databases varun_33 Linux - Software 2 02-03-2005 12:49 AM
Linux Databases McNeesePimp Linux - Software 3 12-21-2004 11:57 PM
Learning databases for IT unholy Linux - Software 0 06-26-2004 05:18 AM
Databases frankyboy4 LQ Suggestions & Feedback 1 08-09-2002 01:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration