How to compare lists in applescripts with same items different sequence?

Hey guys

I want to do this with applescript

i have a list with items originlist [pear,banana, strawberry]

i want to make this list superlist [banana,pear, strawberry] with essentially the same items correspond with it when i say

if originlist equals superlist

because i want them to state that is true when they posses the same items but in different sequence.

Could anyone please help me?:)

It is very much appreciated:D

Thank you so much in advance

Posted on May 28, 2012 8:42 AM

Reply
6 replies

May 28, 2012 9:01 AM in response to s.i.m

The simplest way to do this is to run through all the items of one list and see if they are in the other list:


set originList to {"pear", "banana", "strawberry"}

set superList to {"banana", "pear", "strawberry"}

checkEquivalence(originList, superList)


on checkEquivalence(list1, list2)


-- check if list are the same size; can't be equal if they are not

if (count of list1) ≠ (count of list2) then return false

repeat with thisItem in list1


--check if each item in origList is in superList; set

if thisItem is not in list2 then

return false

end if

end repeat

return true

end checkEquivalence


If you have very large lists this will not be efficient - you'd need to use some optimizing tricks - but for shortish lists this should be fine.


The other way to do it would be to sort the two lists and compare them directly. This is more difficult: either you have to implement an applescript sort routine (there are efficient ones on the web if you google, but they are hard to wrap your head around unless you're an applescript pro), or you have to download and install the Satimage osax and use its sort routines.

May 28, 2012 10:38 AM in response to red_menace

I would also like to reply to TWTWTW,

I am dealing with a list consisting of other lists that contain each 5 items

that list contains about 5000 other lists with 5 items. I can just google applescript sort routines?:S I am willing to become a pro:)


How would i compare records?


to be exact i do this for poker, i have a lot of combinations of cards, and i want to link the originlist to a database of a lot of lists that contains one with exactly the same cards but maybe in different sequence. It is to calculate exact odds of winning which is my ultimate goal.


Thank you so much for your help guys, i appreciate it so much.

May 28, 2012 11:03 AM in response to s.i.m

Well, the applescript go-to guy for sorting algorithms is Nigel Garvey. You can get his original quicksort routine, along with some excellent discussion of its mechanics, here at MacScripter, and you can find his complete assortment of sorting routines (which includes things like sorting lists of lists) here at softpedia. A Dose of Sorts is the one you want, though his other scripts are interesting and useful as well.


If you prefer, you can download the Satimage osax from this link. Just place it in ~/Library/ScriptingAdditions and open its dictionary in the applescript editor. The command you would use would be sortlist, down near the bottom. It would be marginally faster than the optimized applescript sort routines and easier to understand and use, but may not be quite as versatile.


However, before you can make the sorting method work you need to know the ordering of the cards in the online database. I assume it would be first by number and then by suit, but you have to be sure so that you can sort your lists into a comparable ordering.


I'll add that the normal way of computing probabilties is to use probabilty equations (combinatorics). Is there some reason you're trying to brute force it? There are a lot of permutations of five cards from a 52 card deck, so your approach is going to take a long time.

May 28, 2012 11:06 AM in response to s.i.m

A record is a collection of unordered properties, so as long as the properties and their values are the same it doesn't matter what the order is, for example:


setoriginListto {fruit1:"pear", fruit2:"banana", fruit3:"strawberry"}

setsuperListto {fruit2:"banana", fruit3:"strawberry", fruit1:"pear"}


originList = superList--> true

Presorting any given hand before comparing it to the database would probably be the way to go since there aren't that many cards in a hand, but it would depend a bit on how the cards are valued/enumerated and how the database is structured.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to compare lists in applescripts with same items different sequence?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.