0 Favourites

Text search

  • 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!

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thanks for sharing. Did you look at using a Dictionary instead of an Array?

  • 'yes, but as far as I can tell a Dictionary is not a dictionary, it's more like a set of named variables.

  • 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.

  • 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....

  • here's the capx I used.

    [attachment=0:2xcs4gk1][/attachment:2xcs4gk1]

  • 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.

  • 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.

  • 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?

  • 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.

  • After you've read and converted, why not just save as json, then import the file into your project. You could then just Ajax request the file and your off.

  • Which was just what I ended up doing.

    solved, I think.

    There is nothing obvious about retrieving data from webstorage outside a project, so I worked around it.

    The way to get 32000 words into an array is:

    1. read the data from a text file with newline as separator into an array using:

    Import the text file into the project.

    AJAX request the file.

    repeat tokencount(AJAX.Lastdata,newline) times

    Array push back tokenat(AJAX.LastData,loopindex,newline) on X axis.

    (this will take close to a minute)

    2. Use NodeWebKit and run preview with NodeWebkit as selected browser.

    Save (on click of a button or whatever) to file , with Array.AsJson as content.

    3. This gives a txt file, correctly json-formatted, which can now be imported into project, AJAX requested and loaded directly into the Array. (Remember to use "AJAX on completed" before attempting a load into Array with

    Array Load from Json string AJAX.lastdata.

    And this only takes a second.

    Well, I learned a bit from this, and thanks to codah and zenox98 for nudging me along.

  • I think what I did was log the JSON text to the debug console then just copy and paste it to a text file and put it in the files folder under my project folder, reimporting it with Ajax.

  • Which just goes to prove that C2 and its users are flexible in approaches to a challenge.

  • codah

    here's the capx I used.

    [attachment=0:38vvom5f][/attachment:38vvom5f]

    Hello,

    Would you be so kind to share that your capx again?

    Thanx!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
[Plugin] tag text
188 25,447
RBuster's avatar
RBuster
Unread hot topic
118 32,903
rexrainbow's avatar
rexrainbow
Unread hot topic
102 26,817
HERBERT HEINZ's avatar
HERBERT HEINZ