How do I compare entire lists?

Get help using Construct 2

Post » Fri Jul 25, 2014 1:47 am

I want to check if list 1 at least contains all of the items in list 2, regardless of the order. List 1 could have the same elements in the same order, the same elements in a different order, or the same elements with additional elements and it should return True.
How would I do this?
B
5
S
1
Posts: 31
Reputation: 439

Post » Fri Jul 25, 2014 3:13 am

The best way that comes to my mind is to sort both lists (Make copies if you need the unsorted data) and compare those using some loops.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Fri Jul 25, 2014 4:30 am

Are there any functions to help organize alphabetically?
B
5
S
1
Posts: 31
Reputation: 439

Post » Fri Jul 25, 2014 4:44 am

@Zygorithm try to look at array. Array have condition that can check whether ArrayA.CurValue exist in ArrayB. And there's action to manipulate the array, to sort it. Arranging integer works for me (only ascending supported) but I haven't tried sorting strings.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Fri Jul 25, 2014 11:34 am

@Zygorithm ... I'm not sure why you would have to sort the data if you just want to compare lists. For example, if you want to see if all elements in ListB are in ListA, just do a loop against ListB and then find() against ListA using loopindex .... Something like this:

add global variable "ListContainsAll" = "true"
add system loop For "ListCompare" from 1 to tokencount(listB, "," ) [assumes your list delimiter is a comma]
add sub event condition compare two values: find(ListA, tokenat( ListB, loopindex("ListComapre"), ",") ) = -1 --> set ListContainsAll ="false"

If you run the above and all elements in ListB are in ListA, you should end up with "ListContainsAll" still true, otherwise false.
Last edited by kbtbc on Fri Jul 25, 2014 12:17 pm, edited 1 time in total.
B
11
S
2
Posts: 86
Reputation: 1,001

Post » Fri Jul 25, 2014 12:16 pm

Here is a capx demo of the above.

EDIT: If you need to be more precise and to compare against each element of ListA ( which may be necessary depending on your data), your sub event would be a second loop, comparing each element individually.

For example if ListA = "onething,somthingelse" ... and ListB = "some,anotherthing" -- the capx would still return true (because find(ListA, "some") would still be true - "some" is in "somethingelse") ... so if there is potential 'data overlap' in your lists, you would need to use a second inner loop to explicitly compare each element.

... I hope this helps.
You do not have the required permissions to view the files attached to this post.
Last edited by kbtbc on Fri Jul 25, 2014 3:17 pm, edited 2 times in total.
B
11
S
2
Posts: 86
Reputation: 1,001

Post » Fri Jul 25, 2014 1:15 pm

...update: actually the 'more precise' method turns out to be more complicated than I thought, because again there is no 'tokenfind' type of feature -- you may very well have to load both lists into arrays. I'll let someone more experienced answer that, but @Ashley this is an example of my request for fleshing out the token functions. Why not have tokenfind or tokencompare built in? (or tokenappend, tokendelete, tokensort, etc for that matter?... ) ... it would make working with lists so much easier.

(For an example of a development tool that does it right with lists, check out ColdFusion's list functions -- it would also appear C#, C++, etc has plenty of built in list methods.)
B
11
S
2
Posts: 86
Reputation: 1,001

Post » Fri Jul 25, 2014 3:06 pm

@kbtbc something to be aware of is that your method gets exponentially more expensive the longer the list is. Sorting then comparing gets expensive a lot more 'slowly'.

The 'bruteforce' method of looping through 'B' for every entry in 'A' should work fine provided your list is guaranteed to be small.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Fri Jul 25, 2014 4:13 pm

Well, I ended up assigning list 2 elements to a temp array and looping the elements in list 1 to compare against temp. I created a local variable for the length of temp list. Then I created another local variable for how many matches I found. Then I looped through list 1 and added 1 every time there was a match in list 2. If the number of matches equals the length of the temp array, then it returns true. As for expensiveness, the temp array size and list 1 size will never exceed 5 or have any duplicate data so I think it could be done reasonably quickly.
Thanks for everyone's help!
Construct 2 does not have any way to isolate array functions to a Z subsection of a 3D array (at least not that I can see from the documentation), which would have made things a lot easier.
Also, Construct 2 should add booleans. It's very strange having true and false as strings.
B
5
S
1
Posts: 31
Reputation: 439


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 25 guests