CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Comparing and merging two files

 



ZD
New User

Sep 5, 2002, 4:47 PM

Post #1 of 8 (1211 views)
Comparing and merging two files Can't Post

Hi,

I am working on a simple script that synchronizes 2 lists. Where the "New" List can override the records of the "Old" list for those records with equal values in the "Main Column". Records in a single list (not in both) are all included in the resulting list.

Example: Here 2 lists, each has to columns are to be synchronized based on the values in column 1.

OLD List = {A,1},{B,2},{C,3},{D,4},{E,5}

NEW List = {A,1}, {B,6},{C,3},{D,7},{F,9}

The result should be

Result = {A,1}, {B,6},{C,3},{D,7},{F,9},{E,5}

Assuming that I don't know the count of records in each list, how can I do this?

Any hints?

Thanks



thebitch
User

Sep 6, 2002, 12:47 AM

Post #2 of 8 (1206 views)
Re: [ZD] Comparing and merging two files [In reply to] Can't Post

let's see the script.
let's see some sample data (that's not perl)

I'd use a hash and have the keys be the
"{A,1}" stuff.

this is common perl idiom.

do something like


Code
 
for my $key(@List,@List2) {
$FatHash{$key}++;
}

@MergedList = keys %FatHash;



Paul
Enthusiast

Sep 6, 2002, 2:35 AM

Post #3 of 8 (1204 views)
Re: [thebitch] Comparing and merging two files [In reply to] Can't Post

Or more simply:


Code
@fathash{@list1,@list2}++; 
@merged = keys %fathash;


It could probably be shortened more but I need breakfast :)


(This post was edited by RedRum on Sep 6, 2002, 2:35 AM)


uri
Thaumaturge

Sep 7, 2002, 10:18 PM

Post #4 of 8 (1197 views)
Re: [RedRum] Comparing and merging two files [In reply to] Can't Post


In Reply To
Or more simply:


Code
@fathash{@list1,@list2}++; 
@merged = keys %fathash;




it would help if you posted legal code.

++ doesn't work on lists or arrays or hash slices.

the idiom you seek is:


Code
@fathash{@list1,@list2} = () ; 
@merged = keys %fathash;


and the OP would need more than that if he wants to filter elements based on a field.

uri


Paul
Enthusiast

Sep 8, 2002, 8:42 AM

Post #5 of 8 (1194 views)
Re: [uri] Comparing and merging two files [In reply to] Can't Post


Quote
it would help if you posted legal code.


*ego alert*

You'll find that it does work as expected.


(This post was edited by RedRum on Sep 8, 2002, 10:04 AM)


jryan
User

Sep 8, 2002, 11:53 AM

Post #6 of 8 (1187 views)
Re: [ZD] Comparing and merging two files [In reply to] Can't Post

For an easier (and more accurate) method of merging hashes, take a look at the excellent Hash::Merge


thebitch
User

Sep 9, 2002, 1:28 AM

Post #7 of 8 (1185 views)
Re: [RedRum] Comparing and merging two files [In reply to] Can't Post

well I would've expected the value of each key to be
1, but I know from experience that's not what happens.
(cause ++ don't work like that)

it's true in this case it doesn't matter,
but this is how rumors get started ;)


Paul
Enthusiast

Sep 9, 2002, 2:15 AM

Post #8 of 8 (1183 views)
Re: [thebitch] Comparing and merging two files [In reply to] Can't Post

It doesn't make all keys one but it does have an effect which you will see if you try it out.

++ will make the final key have a value of one which is all thats needed for it to do what I wanted. Without ++ it will not work.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives