extream newbie problem: Array index out of range

Monkey Forums/Monkey Beginners/extream newbie problem: Array index out of range

drond(Posted 2014) [#1]
When i try to compile this code i get a "Array index out of range" error message but i can't find any errors in the code... it's driving me crazy.

Function Main:Int () 
	
	Local numbers:Int[] = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
	
	
	For Local i:Int = 0 Until numbers.Length() 
		Print(numbers[i])
	End 

	Return 0	
		
End Function 



drond(Posted 2014) [#2]
the console spits out this error message
Monkey Runtime Error : Array index out of range
C:/MonkeyX77a/Projects/Sandbox/test.monkey<16>



drond(Posted 2014) [#3]
I pushed the kill-button and recompiled and now it's working =D


MOBii(Posted 2014) [#4]
^^


therevills(Posted 2014) [#5]
That's strange... did you change anything else or the build target (release and debug)?

Anyway if you didnt know you can also access the values of an array using EachIn:
	Local numbers:Int[] = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
	
For Local i:Int = Eachin numbers
	Print(i)
End



Gerry Quinn(Posted 2014) [#6]
I don't know if it still happens, but there used to be a glitch where you could compile but a previous version would run (I think only HTML5 and/or Flash). So you could fix a bug and not see the fix.

Or it might have been Just One Of Those Things.


Beaker(Posted 2014) [#7]
Oops .. Losing my mind. :)


Reaper(Posted 2014) [#8]
shouldnt it be
For Local i:Int = 0 Until numbers.Length() - 1 
?
As in your example array length is 11, but arrays start with index 0, so your doing a for loop from 0 to 11 ie 12 indexes.


Raph(Posted 2014) [#9]
Until goes from start to max -1
To goes from start to max