About Monkey 2 › Forums › Monkey 2 Programming Help › Creating new arrays
Tagged: array
This topic contains 8 replies, has 5 voices, and was last updated by 
 degac 2 years, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
May 28, 2016 at 4:57 am #832
Ok, really basic. How do you create a new array and add all values at once (without iterating)?
In Monkey1 it used to be:
1С (Запрос)12Local frames := [0,1,2,3,4]In Monkey2 that doesn’t work. I also tried this:
1С (Запрос)12Local frames := New Int[5]frames = [0,1,2,3,4]Doesn’t work either.
Help!
May 28, 2016 at 6:03 am #833+1I really love (in Bmax) thisMonkey1<del>Local def$[]="alfa,beta,gamma".Split(",")</del>I don’t know if MX2CC can ‘understand’ this type of situation or needs a ‘creation step’ before (I mean how many items can the array store initially).At least something to ‘feed’ the array in some way could be handyMonkey123<del>Local def:=New String[10]def=Array("alfa,beta,gamma".Split(","))</del>No matter, I found the solution on the MonkeyX forum (referring at a blog post here :D)!
In any case I found that this works
Monkey123456789101112131415161718#Import "<std>"Using std..Function Main()Local w:=New Int[](1,2,3)Print w[0]+" - "+w[1]End#Import "<std>"Using std..Function Main()Local sa:=New String[]("alfa","beta")Print sa[0]+"-"+sa[1]EndSo Int[] and String[] can be ‘instanced & resized’ automatically depending on the items passed
This one doesn’t work (
Monkey12345678910#Import "<std>"Using std..Function Main()Local sa:=New String[]("alfa,beta".Split(","))Print sa[0]+"-"+sa[1]EndI need to specify the size of the array
Monkey1234567891011#Import "<std>"Using std..Function Main()Local sa:=New String[2]sa="alfa,beta".Split(",")Print sa[0]+"-"+sa[1]EndMaybe (probably!) I missed something obvious in the syntax.
May 28, 2016 at 8:05 am #834This works:
Monkey1234Local sa:= "alfa,beta,gamma,delta".Split(",")For Local n := Eachin saPrint nNextI think String.Split() already returns a new array, so you don’t have to use “New” or specify the size before calling it!
And yes, I posted in the Monkey-X forums as well, but I like posting here since it can be useful to people starting on Monkey 2.
Cheers!
May 28, 2016 at 10:41 am #837I think String.Split() already returns a new array, so you don’t have to use “New” or specify the size before calling it!
Thanks – I know I’ve did something wrong in the syntax!
I think I’ll add these informations directly in the modules section/monkey-types/array http://monkey2.monkey-x.com/modules-reference/
June 29, 2016 at 1:03 am #1274August 27, 2016 at 4:15 pm #3509OK, after trying & searching I need help.
How we can ‘resize’ an array?
Monkey1234567891011121314151617181920212223Namespace myapp#Import "<std>"Using std..Function Main()Local nn:Float[]Print "GetSize: "+nn.GetSize(0) 'there's nothing! OKPrint "Length : "+nn.Lengthnn=nn.Slice(0,10) 'should 'resize' to 10 items???' nn[0]=1.0 'of course, array out of index...Print "GetSize: "+nn.GetSize(0) 'same, 0Print "Length : "+nn.LengthEndAugust 27, 2016 at 5:25 pm #3510> Method Slice:T[](from)
or
> Method Slice:T[](from:Int,term:Int)I think you need to do a CopyTo:
> Method CopyTo:Void( dstArray:T[], srcOffset:Int, dstOffset:Int, count:Int )I believe you have to create a temporary variable array of the desired length as I don’t see any other way.
or you can simply resize an empty array:
> nn = New Float[10]August 27, 2016 at 8:21 pm #3512Slice() can shrink an array but it can’t grow it – I still need to add a proper Resize().
August 28, 2016 at 6:46 am #3521OK, thanks
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.