Compare if string contains integer

Get help using Construct 2

Post » Tue Jan 21, 2014 1:44 pm

Hey, thats something terribly easy yet Im having hard time doing it with construct2.

"1,6,17,3" - lets say I need to check if it contains number 7. I cant just check if 7 is in string because there is very high chance of false positive. I did want to do what I was used to in any programming language wchich means splicing text by , separator and put all results into array, then check if value is contained in array but in construct2 its just awfully messy, long, and pretty ugly.

Any ideas how I can do that faster/simpler way?
B
22
S
7
Posts: 94
Reputation: 1,783

Post » Tue Jan 21, 2014 1:50 pm

In the manual you can find system expressions you can use with text-objects.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jan 21, 2014 2:04 pm

If you string returns like you show you could check for the commas also: ,7, which would not give any other numbers, like 17, 27, etc.

find(yourText,",7,")

will return > -1 if found (returns the index position)CincyPlanet2014-01-21 14:08:01
B
5
S
1
Posts: 26
Reputation: 479

Post » Tue Jan 21, 2014 2:04 pm

[QUOTE=LittleStain] In the manual you can find system expressions you can use with text-objects.[/QUOTE]

I dont want to be ungratefull but thats just not helpfull at all. I know about manual and Im reading it whenever im in doubts.

I would be very happy for complete answer with directions what to do.
B
22
S
7
Posts: 94
Reputation: 1,783

Post » Tue Jan 21, 2014 2:07 pm

[QUOTE=CincyPlanet] If you string returns like you show you could check for the commas also: ,7, which would not give any other numbers, like 17, 27, etc.
[/QUOTE]

No, actually you cant. You cant ever assume its going to be written that way since there are 4 ways to do it.

7 will get false positive with 17
,7 will get false positive with ,71
7, will get false positive with 17,
,7, will never get false positive but there is only 25% chance it will have this format instead of other 3.PKrawczynski2014-01-21 14:08:51
B
22
S
7
Posts: 94
Reputation: 1,783

Post » Tue Jan 21, 2014 2:11 pm

Maybe you could also check before and after the 7? If there is a comma or nothing (index value 1 or text length -1)?
B
5
S
1
Posts: 26
Reputation: 479

Post » Tue Jan 21, 2014 2:15 pm

[QUOTE=CincyPlanet] Maybe you could also check before and after the 7? If there is a comma or nothing (index value 1 or text length -1)?[/QUOTE]

And what about checking integers like 25,125,1257,13643 etc? as you see this solution doesnt scale very well and I have to dismiss it. I did thought about that.
B
22
S
7
Posts: 94
Reputation: 1,783

Post » Tue Jan 21, 2014 2:18 pm

I see, tough one...Well, I think, you would only have to check the first index: 7, and the last one.

Looking at the expressions available maybe these could help also. You could use the comma separator, check the first and last value, and info not found use the ,7,?

tokenat(src, index, separator)
Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

tokencount(src, separator)
Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.

Im not sure if you could send the list to an array and then cycle through that also...just kind of brainstorming at this point. CincyPlanet2014-01-21 14:21:46
B
5
S
1
Posts: 26
Reputation: 479

Post » Tue Jan 21, 2014 2:23 pm

Would something like this work?

Create a group and add a local variable.

set the local variable to tokencount(text.text,",")

repeat tokencount times
system compare two values : tokenat(text.text, local variable, ",") = 7
- do something
else - system subtract 1 from local variable

ok, I made an error here, I'm sure, but it should be something along the realms of this.LittleStain2014-01-21 14:25:58
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jan 21, 2014 2:27 pm

[QUOTE=LittleStain] Would something like this work?

Create a group and add a local variable.

set the local variable to tokencount(text.text,",")

repeat tokencount times
system compare two values : tokenat(text.text, local variable, ",") = 7
- do something
else - system subtract 1 from local variable

ok, I made an error here, I'm sure, but it should be something along the realms of this.[/QUOTE]

Its pretty good but what about integers like 12,125,1255 etc? they have more than one number in them.


Really weird, I thought it would be pretty common in construct2 and easily fixable issue.PKrawczynski2014-01-21 14:27:55
B
22
S
7
Posts: 94
Reputation: 1,783

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests