How to reduce a global list to 0 ?Monkey Forums/Monkey Beginners/How to reduce a global list to 0 ?
| I'm using the following code to dynamically grow a string dependent on the amount of items in a separate list:|
red_foot_x_list = red_foot_x_list.Resize(red_foot_x_list.Length() +1)
What is the syntax to reduce it to 0? Going to try the following:
red_foot_x_list = red_foot_x_list.Resize(red_foot_x_list.Length() - red_foot_x_list.Length() )
| I take it that you are using an Array and not a real List data type?|
To clear an array completely to zero elements use.
Edit: Trying to use sub traction that gives a value of 0 and then trying to use that value to resize an array will not resize the array. Your basically doing
a=a+0 or a=a-0
a still is 5
Strict Global myArray:Int Function Main:Int() ' Fill the array with 10 random ints Local s:String myArray=myArray.Resize(10) For Local i:Int=0 To myArray.Length()-1 myArray[i]=Rnd(100) s+=myArray[i]+" " Next Print "Current Array Length is "+myArray.Length() Print s ' Halve the array Local current:Int=myArray.Length() myArray=myArray.Resize(myArray.Length()/2) ' Take care when using divisions s="" For Local i:Int=0 To myArray.Length()-1 s+=myArray[i]+" " Next Print "Halved the array from "+current+" to "+myArray.Length() Print s ' Clear the array myArray= s="" For Local i:Int=0 To myArray.Length()-1 s+=myArray[i]+" " Next Print "Clear the array. Which should be zero length > "+myArray.Length() Print s Return 0 End Function
| [Deleted duplicate post] |
| if you want an array-like structure that you can grow at will, use a Stack. Resizing an array for every element you add is horribly inefficient in terms of time and memory fragmentation. A Stack contains an array that resizes automatically in a smarter fashion.|
[And if you want to handle it yourself, look at the code for Stack; it's written in plain Monkey.]