parsing a text string to an array?

Get help using Construct 2

Post » Fri Aug 24, 2012 7:24 am

I've been.racking my brain trying to figure this out for a week so I thought I'd post here and finally give up and ask for help lol, here's what I'm trying to

I want to be able to take a string of text, example: "wwwbbbss" and parse it into an array where each one character in the string parsed to a seperate array value,(w in 1,0 thru 3,0 b in 4,0 etc.)

Basically I'm trying to make a level editor that I can make txt levels with at work on my phone lol then email em to myself and play on construct lol

Any help would be greatly appreciated
Thanks!
B
6
S
2
G
3
Posts: 55
Reputation: 2,688

Post » Fri Aug 24, 2012 7:35 am

I'm actually doing something very similar to that, too!
You're probably looking for the "tokenat()" expression.

[QUOTE=The Construct 2 Manual]tokenat(src, index, separator)

Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.[/QUOTE]Zero62012-08-24 07:39:03
B
13
S
4
Posts: 117
Reputation: 2,242

Post » Fri Aug 24, 2012 4:32 pm

from the example i made i'd need to have my string set to something like, "w|w|w|b|b|b|s|s"?
and then, to retrieve an item i'd enter something like
tokenat("w|w|w|b|b|s|s",6,"|")
would return the first S?

B
6
S
2
G
3
Posts: 55
Reputation: 2,688

Post » Fri Aug 24, 2012 4:34 pm

the count starts with 0, so it would be the second "s"

EDIT: If you like to work without separators, you can use the "left" and "right" expressions.

Based on your example:
wwwbbbss

to get the 4th entry, use this code:

right(left("wwwbbbss",4),1)

this would return the first "b"


EDIT2: mid can be used too:
Same example as above but the expression would be:
mid("wwwbbbss", 4, 1)Ubivis2012-08-24 16:39:30
B
42
S
9
G
6
Posts: 437
Reputation: 5,626

Post » Fri Aug 24, 2012 4:52 pm

tokenat is the way to go if you ever need more than a single letter/digit to represent somethin
B
13
S
5
G
2
Posts: 306
Reputation: 3,262


Return to How do I....?

Who is online

Users browsing this forum: Zebbi and 23 guests