Thursday, December 5, 2019

#1 2019-11-09 05:30:41 am

ldicroce
Member
Registered: 2017-11-25
Posts: 137

Compare lists of records

I have two lists of records with this structure:

Applescript:

{pdfName:ThisPDFname, DOIofPDF:myDOI}

Which is the fasted way to compare them? I want to remove same records which are present in both lists.

Thanks

Last edited by ldicroce (2019-11-09 05:31:04 am)

Offline

 

#2 2019-11-09 06:54:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6062

Re: Compare lists of records

So you common items removed from both lists?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2019-11-10 01:35:02 am

ldicroce
Member
Registered: 2017-11-25
Posts: 137

Re: Compare lists of records

Shane Stanley wrote:

So you common items removed from both lists?


yes. For example:
LIST1
{pdfName:A.pdf, DOIofPDF:1}
{pdfName:B.pdf, DOIofPDF:2}
{pdfName:C.pdf, DOIofPDF:3}

List1
{pdfName:A.pdf, DOIofPDF:1}
{pdfName:B.pdf, DOIofPDF:3}
{pdfName:D.pdf, DOIofPDF:4}

Only
{pdfName:A.pdf, DOIofPDF:1}
should be removed from both lists.

Offline

 

#4 2019-11-10 05:40:37 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6062

Re: Compare lists of records

You could use this:

Applescript:

set list1 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:2}, {pdfName:"C.pdf", DOIofPDF:3}}
set list2 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:3}, {pdfName:"D.pdf", DOIofPDF:4}}
set list1New to {}
set list2New to {}
repeat with anItem in list1
   set anItem to contents of anItem
   if list2 does not contain {anItem} then set end of list1New to anItem
end repeat
repeat with anItem in list2
   set anItem to contents of anItem
   if list1 does not contain {anItem} then set end of list2New to anItem
end repeat
return {list1New, list2New}

For very long lists, this might be quicker:

Applescript:

use framework "Foundation"
use scripting additions

set list1 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:2}, {pdfName:"C.pdf", DOIofPDF:3}}
set list2 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:3}, {pdfName:"D.pdf", DOIofPDF:4}}
set list1 to current application's NSMutableArray's arrayWithArray:list1
set list1Copy to list1's |copy|()
set list2 to current application's NSMutableArray's arrayWithArray:list2
list1's removeObjectsInArray:list2
list2's removeObjectsInArray:list1Copy
return {list1 as list, list2 as list}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-11-10 05:54:04 am

ldicroce
Member
Registered: 2017-11-25
Posts: 137

Re: Compare lists of records

Shane Stanley wrote:

You could use this:

Applescript:

set list1 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:2}, {pdfName:"C.pdf", DOIofPDF:3}}
set list2 to {{pdfName:"A.pdf", DOIofPDF:1}, {pdfName:"B.pdf", DOIofPDF:3}, {pdfName:"D.pdf", DOIofPDF:4}}
set list1New to {}
set list2New to {}
repeat with anItem in list1
   set anItem to contents of anItem
   if list2 does not contain {anItem} then set end of list1New to anItem
end repeat
....

This was my error. I didn't "set anItem to contents of anItem". I just used directly "anItem"


For very long lists, this might be quicker:

Applescript:

use framework "Foundation"
use scripting additions

And this will be useful since my list is more that 1000 records.
Thanks a lot !!!!!

Last edited by ldicroce (2019-11-10 05:56:46 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)