How do I get the index of a token?

Get help using Construct 2

Post » Sat Jun 18, 2016 7:40 am

Hi!

I want to use this expression in reverse.
Code: Select all
tokenat(src, index, separator)


My string is "text1,text2,text3".
Now I want to get the index of "text2". Is there a way to do this with an expression?
Or do I need to make a function on my own for this?`

Thank you!

Regards
Andy
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,134
Reputation: 28,081

Post » Sat Jun 18, 2016 9:18 pm

You'll need to do this manually. One simple idea is to use a loop like:


Number index=-1

Repeat tokencount(text, ",") times
Tokenat(text, loopindex, ",") = "fish"
--- stop loop
--- set index to loopindex
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Sun Jun 19, 2016 5:34 am

R0J0hound wrote:You'll need to do this manually. One simple idea is to use a loop like:


Number index=-1

Repeat tokencount(text, ",") times
Tokenat(text, loopindex, ",") = "fish"
--- stop loop
--- set index to loopindex


Thank you @R0J0hound :)

I'll try that!
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,134
Reputation: 28,081

Post » Sun Jun 19, 2016 5:43 am

My version is just a variant of what R0j0hound already proposed.

Simply
For "index_name" from 0 to tokencount("text1,text2,text3",",") - 1
--- (condition) tokenat("text1,text2,text3",loopindex("index_name"),",") = "text2"
--- (Action) Collect dictionary: Add key "Index_var" with value loopindex("index_name")

*replace dictionary, index_var, index_name and string as you wanted
B
36
S
18
G
11
Posts: 248
Reputation: 8,694

Post » Sun Jun 19, 2016 6:11 am

Sethmaster wrote:My version is just a variant of what R0j0hound already proposed.

Simply
For "index_name" from 0 to tokencount("text1,text2,text3",",") - 1
--- (condition) tokenat("text1,text2,text3",loopindex("index_name"),",") = "text2"
--- (Action) Collect dictionary: Add key "Index_var" with value loopindex("index_name")

*replace dictionary, index_var, index_name and string as you wanted


Thank you for your example :) I'll give both a try :)

Thank you!
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,134
Reputation: 28,081


Return to How do I....?

Who is online

Users browsing this forum: mcscooter and 28 guests