Text search

Discussion and feedback on Construct 2

Post » Thu Jun 26, 2014 11:31 am

Just thought I¨d share my discovery.
I tried to put a list of 33000 words into an array and search for a specific word. (For a word game)
This was rather sluggish , so, not thinking it would work, I put the entire word list into a string (a Global Text), separated by ";".
Using find(MyList, "WORD") is instant, and solved the whole problem.
If the WORD is not in the string it returns -1, also instantly, which is sufficient info for a word making game.
So if you just need to check if a word is valid, this is the way to go.

And way to go, Construct team. You make my work a joy!
B
5
S
1
Posts: 23
Reputation: 471

Post » Thu Jun 26, 2014 12:50 pm

Thanks for sharing. Did you look at using a Dictionary instead of an Array?
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jun 26, 2014 6:18 pm

'yes, but as far as I can tell a Dictionary is not a dictionary, it's more like a set of named variables.
B
5
S
1
Posts: 23
Reputation: 471

Post » Thu Jun 26, 2014 10:48 pm

Think of a Dictionary as an Array with 2 values per entry, that are associated with each other. That's why in programming terms it's called an 'associative array'. Each entry has a 'key' (text) and a 'value' (number).

Anyway, I did some testing and created an Array of 33,000 words and found that a seach for even the last word is pretty much instantaneous using the "ContainsValue" condition of the Array.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Fri Jun 27, 2014 6:21 am

That´s interesting, I may have been going about it clumsily. Array is clearly the best way, because it searches for the full and correct instance of a word. My find(ListText,Word) is flawed by the fact that if it finds any suggested combination of letters it returns true. E.g. "XPLAI" would be found, being part of EXPLAINED, Boggle enthusiasts would shudder in disgust. :-)
But how did you get the list into the Array quickly? I tried to read from the string using
for ReadList 0 to 33000
MyArray(loopindex)= tokenat(ListText,loopindex,";")

and it took quite a while, 35 seconds, and then crashed....
B
5
S
1
Posts: 23
Reputation: 471

Post » Fri Jun 27, 2014 7:27 am

here's the capx I used.

array_search.capx
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Fri Jun 27, 2014 6:39 pm

Now that´s instant! Thanks!
Do you, by any chance, have any idea of how to read a 32000 line txt file into an Array?
It should be possible if it´s converted to a JSon format, but I don´t really know how to make that.
I tried the method I mentioned above, reading tokenat but it´s too slow.
B
5
S
1
Posts: 23
Reputation: 471

Post » Sat Jun 28, 2014 7:39 am

Make a small Array, say just a few words. Save it as JSON, then you can see the format with a text editor, and just add your 32000 words in that format. Then you can re-load into an Array.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sun Jun 29, 2014 2:00 pm

Clever! I can save an Array to JSon using WebStorage (and reload it). Bu I'm having a hard time finding out where it is stored as a file. Any ideas?
B
5
S
1
Posts: 23
Reputation: 471

Post » Sun Jun 29, 2014 8:13 pm

I'm getting closer. :-)
I found out how to read the data from a text file into an array, which takes 45 seconds.
After that searching the 32000 cells is pretty much instant.
I can also save the array as json to webstorage once, and then in subsequent runs read it back from webstorage, which is also pretty much instant.
What I'm banging my head against now is:
I want to find the saved array in json form, which webstorage has buried somewhere, and include it as a project file to avoid the timeconsuming conversion, which I can not expect users to have patience with.

So my question is where Webstorage has saved the file? So I can get at it outside the editor.
B
5
S
1
Posts: 23
Reputation: 471

Next

Return to Construct 2 General

Who is online

Users browsing this forum: Unconnected and 12 guests