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
94
S
33
G
30
Posts: 1,099
Reputation: 27,645

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
91
S
31
G
102
Posts: 5,234
Reputation: 67,254

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
94
S
33
G
30
Posts: 1,099
Reputation: 27,645

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
94
S
33
G
30
Posts: 1,099
Reputation: 27,645


Return to How do I....?

Who is online

Users browsing this forum: ksiads, R0J0hound and 8 guests