slices broken

Monkey Forums/Monkey Programming/slices broken

Jesse(Posted 2012) [#1]
the code below doesn't resize the arrays:

[monkeycode]

Strict

Function Main:Int()
Local a:String[]=["1","2","3"]
a = a[..6]

Print a.Length

Local b:Int[] = [1,2,3,4]
b = b[..8]
Print b.Length
Return True
End Function

[/monkeycode]


therevills(Posted 2012) [#2]
Looks like this never worked (well I went back to v45c).

You can slice to a smaller array or use Resize to increase the array.


Jesse(Posted 2012) [#3]
I did tried with resizing and it did work.


Jesse(Posted 2012) [#4]
I just found out also that Resize doesn't work if the array is 0 Length.

Function Main:Int()
	Local a:String[]
	a.Resize(8)
	Print a.Length
	Return True
End Function



marksibly(Posted 2012) [#5]
Hi,

This is the way slices work in Monkey, you need to use Resize() to 'grow' an array.

> I just found out also that Resize doesn't work if the array is 0 Length.

Resize returns a new array, so you need to go:

a=a.Resize(8)


Shinkiro1(Posted 2012) [#6]
So in conclusion:
* Use Resize when you want to change the size of an array
* Use Slices when you want to get elements of an array

Both of these operations return a new array and don't effect the one your are performing the operation on.


Samah(Posted 2012) [#7]
Correct. If you want to do some more advanced stuff with arrays, check out the Arrays utility class in Diddy.