LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How to turn numerical strings into arrays?

Root / Programming Questions / [.]

ResetReloadCreated:
Let's say I want to use MID$ to get a number out of a string. How would I convert it into an array%? Here's what I have so far:
INPUT "Generation code: ",G%
?G%

DIM AREAS%[3]
AREAS%[0]=0
AREAS%[1]=0
AREAS%[2]=0

FOR I=0 TO 2
STRING$=MID$(G%,I,1)
AREAS[I]%=STRING%
NEXT
Any suggestions?

I'm assuming the input format is some whole number of arbitrary size, e.g. Generation code: 123. Restricting the input to be only 3 digits long might be a blessing or a curse, I don't know if you absolutely require it to be three digits, or if you're willing to make it any size? What you're effectively doing here is splitting a number into its constituent digits, right?

I'm assuming the input format is some whole number of arbitrary size, e.g. Generation code: 123. Restricting the input to be only 3 digits long might be a blessing or a curse, I don't know if you absolutely require it to be three digits, or if you're willing to make it any size? What you're effectively doing here is splitting a number into its constituent digits, right?
It was just an example so I can figure out how to use it. I'm using input to input about 10 numbers at the moment, and each number in the GENERATION code will determine a landmark such as a tree or flower. Kind of like those old arcade passwords, but for loading a small town with 10 tiles, if that makes sense.

So, you're expecting a 10-digit integer. Okay. Your input line is great. But right after, you first have to get a string version of G%, since it's an integer.
G$=FORMAT$("%010D",G%)
G$ is now a string representing G%'s value a certain way. Let's look at that format string for a sec:
%010D
The FORMAT$ function takes a format string, and then some number of inputs. The inputs are then used to generate a string based on the provided format. This means the input is an integer, and we want it turned into an integer in base-10, ten digits long, with leading zeros. The upshot of this is that if the user inputs a number less than 10 digits, the rest at the beginning are filled in with zeros. 20 would become 0000000020, for example. Since you're always expecting ten digits, we make AREAS% be ten entries.
DIM AREAS%[10]
We don't need to fill it in with zeros, since SB does it automatically. Now we have to split each digit (now characters in a string) and put them inside the array. A simple FOR will do it.
FOR I%=0 TO 9
 AREAS%[I%]=VAL(G$[I%])
NEXT
Instead of doing MID$(G$,I%,1), we can do G$[I%]. Strings support the array-bracket notation; by using it, we get the character at index I% with a lot less code. The VAL function here takes the character and generates the integer it represents. If we give it "0", it returns 0, and so on. It works with entire strings, too. That value is just assigned to our array index, and we're done. This is basically just the process of turning a number into its digits, but with strings instead of math. Altogether the code is:
INPUT "Generation code: ",G%
G$=FORMAT$("%010D",G%)
DIM AREAS%[10]
FOR I%=0 TO 9
 AREAS%[I%]=VAL(G$[I%])
NEXT
You could quite easily turn this code into something that handles an arbitrary number of digits, but I didn't want to burden you with that if you didn't need it.

Thank you so much! It seems to work, mostly, but for some reason if the inputted information doesn't start with 0, repeating anything other than 1 makes it ask me to REDO FROM START. I can work around it, but I'm curious as to what may cause this. Do you know?

Thank you so much! It seems to work, mostly, but for some reason if the inputted information doesn't start with 0, repeating anything other than 1 makes it ask me to REDO FROM START. I can work around it, but I'm curious as to what may cause this. Do you know?
Er, no. I don't think you're describing things properly. Give one specific instance; what code were you using, what did you do, what happened.

Er, no. I don't think you're describing things properly. Give one specific instance; what code were you using, what did you do, what happened.
Sorry. I was using
INPUT "TOWN CODE: ",TC%
TC$=FORMAT$("010D",TC%)

FOR I%=0 TO 9
  AREA%[I%]=VAL (TC$[I%])
 ?AREA%[I%]
NEXT
When I run the program, if I input something such as 7775553332, the program says: Redo from start. If I input 1111111111, 0000000000, or 0987654321 the program does not say Redo from stary. I tested out a few combinations with the first digit being 0 or 1, and only then would 0777555333 work.
Spoiler*forgot a word & detail.

The largest number which can be stored in the integer data type is 2147483647. It has nothing to do with repeated digits, and it's not just 10-digit numbers beginning with '0' that will always pass, also any 10-digit number beginning with '1' and some beginning with '2'. Using a floating point variable and "%010.0F" should do the trick. EDIT: A floating-point variable is signified by a # on the end instead of a %.