FindToken?

For questions about using Classic.

Post » Tue Apr 26, 2011 11:18 am

I don't want to resort to Python if I can help it (I'm having to do that more often than I'd like), but I thought I'd just check with you guys first, just in case it is buried somewhere in Construct.

What I need to do is get the number of matches between a delimiter-separated text variable "a,b,c,d,e" etc, and a given text like "a".

I can obviously do this with events, but what I need is to be able to use it within a condition, like

If FindToken(Global('TextVariable'),"a",",")>0

Which returns true if one or more "a" is found in the Variable.

I can see Get Token and Get Token Count in there, but I can't see a FInd Token or equivalent.
Is there one in there?

Krush.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062

Post » Tue Apr 26, 2011 12:36 pm

It depends on the details. For the specific example, there's
Find(source, string [, start])
It doesn't care about delimiters, but that would not be needed for it anyway.

So, a call to
+ Find(Global('TextVariable'), "a") > 0
-> do stuff if a is in text

will return the first index of occurance or 0
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Tue Apr 26, 2011 3:23 pm

See, I wouldn't have looked for "Find".
I was busy looking at the various text functions for it.

This will work perfectly for what I want.
Thanks again mate.

BTW, I notice that the index returned for the very first letter in the string is 2, instead of what I presumed would be zero.
It doesn't affect what I need to do with Find, as I don't need the returned value, but just out of curiosity, anyone know why?

Krush.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062

Post » Tue Apr 26, 2011 6:48 pm

A string is a string of characters and a character is not just letters or numbers or special characters like comma or quotation mark. The plain ASCII set Construct works with also contains non-printing control chars like backspace or carriage return ([url:3k97o8iv]http://www.asciitable.com/[/url:3k97o8iv]). You won't see those chars although they are in the string and therefore count if you use a function like 'find'.

Why do I tell that? Well, the 'find' expression works correctly. find("a,b,c,d,e", "a") will return 1. If you encounter something else, then your source string does contain a char before the assumed first char. If you load the string from a file, make sure there is no cr or tab or another of those control chars before the first printing char. Sometimes a space is also overseen.

p.s. the first index is 1. If the substring couldn't be found in string, zero is returned.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Apr 27, 2011 10:48 am

It was an error in my code that was returning the wrong index. :)
There's no erroneous characters in my strings as they are assembled in the program rather than from a file.

I finally decided to use the function object to write a function that I can pass the strings to which will then return the amount of matches found.
The main reason for this is that I do need to use delimiters for some of the strings, as there are entries of different lengths in some of them, and using the function object allows me to use the Get Token and Get Token Count to loop through varied lengths of text and get accurate results with the matches.

So far so good. :)

Cheers,
Krush.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests