Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / How to get the Index(char) of token

Author
Message
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2020 08:49 Edited at: 3rd Dec 2020 10:27
Hi all
I want to get the index (char) of a token in an unknown string. How do I do this?

Lets say the unknown string is: "This is an AppGameKit string. AppGameKit is used for this project."

If I use CountStringToken(string$,chr(32) I get 5 tokens.
Token 4 of this string is "AppGameKit"
How do I get the index of "AppGameKit" or put in another way: the CHAR of the "A" in "AGK"

It is important that the string is unknown. (not for the program, but for the coder) therefore reusable.

Tim
Loktofeit
AGK Developer
12
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 3rd Dec 2020 11:08




If the letter you need might be in a different place in the token, use Mid instead of Left.

Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2020 11:37
Sorry, but this is not what I want.
You have just created another "Left" string.
I want to know the INDEX of of the first letter of a token INSIDE a string.

String$ = "I like AppGameKit. The program I use is AppGameKit"

the token number of the first appearance of "AppGameKit" is 3
Then I want to know the INDEX or CHAR of "AppGameKit" in that string.

you just reduced the string to one word.

The important thing here is that the string is unknown for the coder (not for the program)
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2020 12:05
Paul Johnston,

What I am after should maybe be a part of the string commands.
It is similar to "FindString" which returns the index or CHAR of the found string.

maybe something like "findToken"?
findToken(String$, token)

findString in help file:

Returns the index of the first occurrence of findStr in the given string. Index 1 is the first character in the string, returns 0 if not found. By default this is case insensitive, use the ignoreCase parameter to set case sensitivity.
Loktofeit
AGK Developer
12
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 3rd Dec 2020 16:42 Edited at: 3rd Dec 2020 16:53
Then I guess I don't get the reason for using the token. This code will tell you where the first instance of your string starts.



EDIT: Followed you down this path of weird commands when it seems like MID solves this.
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2020 18:19 Edited at: 3rd Dec 2020 18:23
Hi Loktofeit
Thank you for your suggestion.

the command "findString" and "findStringreverse" does the same as your snippet very easily.

findString
https://www.appgamekit.com/documentation/Reference/Core/FindString.htm

findStringReverse
https://www.appgamekit.com/documentation/Reference/Core/FindStringReverse.htm

I thought maybe there was an easy way, something I overlooked In the documentation, to do the same with a token.
I guess I have to make a workaround, like you did.

The reason is that I want to manipulate words in a text object when the mouse hovers over them. like changing their color or animate them.

Tim
Raven
16
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 3rd Dec 2020 18:23
I think the issue at play here is that your confused about what "Token" means, or perhaps more specifically what they're supposed to be used for.
In fairness the AppGameKit Help is absolutely abysmal when it comes to actually being useful to understand terms and how it's commands actually work.

From a simple standpoint a 'Token' is a repeatable Unicode Symbol(s) that can be used to partition Strings.
The most obvious two that you commonly use in every day English is ' ' ( space., which is chr(32) or chr(0x20) ) and '.' ( period., which is chr(46) or chr(0x2E) )
Now what the Token Commands can do is tell you how many of these matched instances appear within a given string., and return a given token index as a string.

So let's say we have your sentence
"This is an AppGameKit string. AppGameKit is used for this project."

Then GetTokenCount( String$, Chr(0x20) ) should return 10.
Token(0) = This
Token(1) = is
Token(2) = an
Token(3) = AppGameKit
Token(4) = string.
Token(5) = AppGameKit
Token(6) = is
Token(7) = used
Token(8) = for
Token(9) = this
Token(10) = project.

You can then acquire this with GetStringToken( String$, Chr(0x20), 3 )., which would return AppGameKit.
And if you want the first letter of that word., well you can either use Left( TokenString$, 1 ) or Mid( TokenString$, 1, 1 )

Something to keep in mind thought is (a) GetStringToken doesn't return the string WITH the token... it only returns what's before the given token (to a preview token, if applicable).
What's more is if you want to handle Multiple Tokens., while you can have multiple they're treated as the same token.
As such, let's say you want to break down the number of Sentences (.) and then Words ( ) ... you will have to do separate passes with the GetStringToken.

Still, keep in mind that Tokens are literally just matched patterns... so for example you could use GetStringToken( String$, "AppGameKit", 2 ) and that'd return "is used for this project."
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 3rd Dec 2020 19:14 Edited at: 3rd Dec 2020 19:16
I have to be clear. As I wrote in my last answer:

I need to know the POSITION of a token in a string, not the string of the token.
I know that a token is not a string.

So lets say the program is fed this string: "this is an AppGameKit program."
If I know the word to look for - in this example "AppGameKit"
then I could use: FindString("this is an AppGameKit program.","AppGameKit")
then the program wil give me 12 and I know the position of that word.


this is the answer I want - and that is great.
but In my case I want to get this answer without knowing what string to look for - only the token to look for.

an implementation of this would be a new command I call: FindToken
for example: FindToken("This is an AppGameKit program",3)
and it would feed me the number 12

Im at the case and will mock up a function soon to show you what I mean.
Virtual Nomad
Moderator
15
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 4th Dec 2020 00:45 Edited at: 4th Dec 2020 01:01
is this closer to what you're looking for?



And, "AGK" is probably not the best example to use on the forum where it will automatically get converted to AppGameKit if it's not surrounded by something other than CHR(32)s

Oops, i think i did what Loktofeit had already done. i can't think of another way to achieve similar, otherwise :/
[My Itch.io Home] [Now Playing]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=AGK] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[Google Forum Search]

Attachments

Login to view attachments
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 4th Dec 2020 01:24
Thanks

The problem with this method is that sometimes the code picks the word "is" and then colors "is" in the first word "This"
So yeah, there is lot of work to get this working. But I really think this should be a part of the core string commands.

Virtual Nomad
Moderator
15
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 4th Dec 2020 01:36 Edited at: 4th Dec 2020 01:39
i see. yah, definitely needs work.

i can see ThisToken$ = Delim$+ThisToken$+Delim$ but there's probably more to account for (like when the Token is first/last). Or, if we wanted "string" to be highlighted vs "string."

also, for anyone else reading, note that MID() starts at 1 while Chars in Text Objects start @ 0, Hence: SetTextCharColor(ThisText,ThisChar-1,255,0,0,255)

it only took a minute to realize but Strings and Text Objects could do with some relationship therapy.
[My Itch.io Home] [Now Playing]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=AGK] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[Google Forum Search]
Timshark
14
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 8th Dec 2020 01:17 Edited at: 8th Dec 2020 15:15
Hi There
Just to finish this Thread
Here is my code that solved the problem - Im sure there is a more effective way.

What I discovered that made my head hurt for a long time was what virtualNomad pointed out in the post:
findString() returns an index that begins at 1 - so the first letter in a string begins at 1
but getTextCharX() and getTextCharY() uses an index that starts at 0 - so the first char begins at 0

I guess the reason for this is that findString() belongs to the string commands and getTextCharX() belongs to the text command

When I understood that I could fin my workaround . Here is the code.



Thanks for the help.

Tim

Login to post a reply

Server time is: 2021-10-19 06:01:22
Your offset time is: 2021-10-19 06:01:22