How do I split string into array?

0 favourites
  • 4 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • Say for example I have some data that reads:

    "Bob|25|Male|Tall|Mary|24|Female|Short|Alex|48|Male|Medium"

    I note that each section is divided into 4. So every 4 tokens is a different Name, and the following 3 are details about said person.

    I want to put this data into an array.

    Now, I've found that when I have data such as:

    "Bob|25|Mary|24|Alex|48|"

    I can sort this data easily by counting the "|" tokens and using a for-loop.

    For 0 to TokenCount-1

    if loopindex%2 = 0

    How do I do this kind of thing when I have more than 2 columns for my data?

    Sorry if I'm not making much sense :) Please ask and I'll try again to explain :)

  • AnD4D

    When I transfer data back and forth to a database, I use a record delimiter and a field delimiter

    so, I would have something like:

    Bob|25|Male|Tall/Mary|24|Female|Short/Alex|48|Male|Medium

    then have a double loop, first to pull out a record (Bob|25|Male|Tall)

    and then a loop to pull out the fields...

  • Ahhh, ok, I may be able to do something for that! Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Set array size to (tokencount/4,4,1)

    Repeat tokencount/4 times

    — set array at (loopindex, 0) to tokenat(loopindex*4+0)

    — set array at (loopindex,1) to tokenat(loopindex*4+1)

    — ... and so on for 2 and 3

    Alternatively you can simplify it further

    Set array size to (tokencount/4,4,1)

    Repeat tokencount times

    — set at (int(loopindex/4), loopindex%4) to tokenat(loopindex)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)