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

Out of range error during arrays managing

Root / Programming Questions / [.]

Stefano_LassandroCreated:
OPTION STRICT
DIM A$[64]
VAR I

A$[0]="abcdefgh"
PRINT (A$[I])[2] 'Error line
I thought that this was going to output "c", but looks like there's an out of range error on the PRINT line. I thought il could work because this works:
A$="abcdefgh"
PRINT A$[2]
'Output: "c"
So, I've three questions: 1) Is MID$ the fastest (working with string arrays) way? 2) In the cases where I can't use MID$, what should I do? 3) Is there a way to "fix" this code? Thanks! And sorry for my bad english : )

Your first code should work so long as I is within the array confines. There's really no other way it could break.

A string array is a list of strings, so if you set A$[0], all the other items in the array are still empty strings so A$[1] is an empty string, and A$[1][2] will be an out of range error

But if I=0 and in the position 0 of the A$ array there is a string, why the first one is not working? It's strange...

Your code works for me in both the 3DS and Switch versions. Is this really all of the code? Did you make a typo somewhere?

Your code works for me in both the 3DS and Switch versions. Is this really all of the code? Did you make a typo somewhere?
Oh, actually separated from the rest of the code works! It's strange becouse it's not in any loop so I doesn't change before PRINT anyway. Dunno why

Does your code look like this, maybe?
OPTION STRICT
DIM A$[64]
VAR I

FOR I=0 TO 9
 '...
NEXT

A$[0]="abcdefgh"
PRINT (A$[I])[2] 'Error line
Note that I will be set to 10 after the loop is finished.