How do I search an array's values for partial match?

Get help using Construct 2

Post » Sun Mar 15, 2015 8:22 pm

Is there a way to do this?

Like say I need to search for all indexes that have a value that is a string, but I only want to search for part of the string.

Example:

I've pushed the following values to the array:

"Oranges 2"
"Oranges 3"
"Grapes 2"
"Oranges 10"

"Oranges 10" is a value I want to keep in there, so I want to use IndexOf with Delete to kill the other two. I can't just pop the front of the array because that would get rid of "Grapes 2" as well. How would I target just the "Oranges 2" and "Oranges 3" values for deletion if I want to do it by searching for "Oranges"? Is there a better way of doing this that I'm missing?
B
18
S
3
G
1
Posts: 35
Reputation: 1,875

Post » Sun Mar 15, 2015 9:14 pm

If you know the word to search, like you say "Oranges" then you can use len("Oranges") to count the letters in the word you are looking for. Then you can trim each word in a search loop of the Array by using left(text, count), so that you only get the word you are looking for.
Frosty Elk AB
www.frostyelk.se
Have no fear, We come in peace!
B
13
S
3
G
1
Posts: 106
Reputation: 1,169

Post » Mon Mar 16, 2015 2:06 am

Hi, thanks for the reply. Can you explain what you mean by "trim each word in a search loop of the array"? I understand the purpose of left and len, but I'm not sure how I would use them in an actual loop.

Would it be like

For "x" from 0 to array.Width
array | Value at loopindex("x") = left(array.At(loopindex("x"), 7)
B
18
S
3
G
1
Posts: 35
Reputation: 1,875

Post » Mon Mar 16, 2015 11:11 am

Yeah, thats what i mean....

if your word is "Oranges" then you ask len("Oranges") and it will give you the answer of 7. Then you use 7 as the left() number as you did in the above case.

So you could set::::: array | Value at loopindex("x") = left(array.At(loopindex("x"), len("Oranges"))
Frosty Elk AB
www.frostyelk.se
Have no fear, We come in peace!
B
13
S
3
G
1
Posts: 106
Reputation: 1,169


Return to How do I....?

Who is online

Users browsing this forum: SputnikCZ and 2 guests