multiple results in array search

Get help using Construct 2

Post » Tue Jun 18, 2013 3:03 pm

According to the manual :
IndexOf, LastIndexOf
Searches the array X axis for a given value and returns the index it is found at, or -1 if not found. IndexOf finds the first matching element, and LastIndexOf finds the last matching element.
I successfully use it.

But I need to find all matching element in a search (more than 2).
How to do that?

I thought of i= array.indexOf(searchstring)+1, compare value until i=array.lastindexOf(searchstring) but I wonder if there's a way to use index and not while loops?
For example, can we re-search index starting at lastresult+1?

I know it might look weird but my array is 1942 long
So far I didn't find that entries are limited which is a very good thing.
I wonder what happens to the browser (or smartphone app) if "you go too far" and how much is "too far".

Thanks for your help!
B
12
S
2
G
3
Posts: 16
Reputation: 2,823

Post » Tue Jun 18, 2013 3:25 pm

In the Array Plugin, both IndexOf and LastIndexOf take only 1 argument : the value to search for, and I'm sorry to say there isn't, in the official implementation, a way to get the result of a search starting from the Nth index.
Anyhow, if you don't know how many items will have the searched value, there is no use of having a list of all the index. Why ? Because to use them, you would have to do a forEach on the list/array. Why not just do a simple forEach on the first array, and do the full iteration only when the value matches what you are looking for ?

Regarding the "too far" consideration, just know that C2 Arrays use javascript arrays to stock datas. Having a big array isn't a problem, as long as the total sum of the datas weight isn't excessive. Having 5k names isn't a problem. Having the base64 code of 100 pictures is. It all depends on how much data is in the array. Arrays are optimised since V8 for very fast access and writing, so lots of elements don't seem to be so problematic now. (more on this here)
B
17
S
8
G
4
Posts: 461
Reputation: 6,087

Post » Tue Jun 18, 2013 5:11 pm

Thanks Guizmus,
Hmm, right, I'll need to improve my search logic because one argument can be found multiple times in index.
My json file is 258k and it's pretty fast on the laptop but I wonder on a mobile device and if it's RAM related too (?).
I also wonder if there's a limit per dimension (x,y,z).
I thought of using long strings or integers to concatenate and do layer search but then I wonder how many digits it can handle before going to lala land and if it's also CPU (mobile battery) intensive.
B
12
S
2
G
3
Posts: 16
Reputation: 2,823


Return to How do I....?

Who is online

Users browsing this forum: gamarros, imhotep22, Nimothar, senecaa, yuji567 and 6 guests