About Monkey 2 › Forums › Monkey 2 Programming Help › Multidimensional Arrays and Copyto?
This topic contains 11 replies, has 5 voices, and was last updated by
Gerry Quinn 1 year, 9 months ago.
-
AuthorPosts
-
June 25, 2017 at 4:16 am #8900
I was wondering how (or if it is possible) to copy a multidimensional array with the “Array.Copyto” command?
At the moment I use for loops to copy the contenst of a multidimensional array.
June 25, 2017 at 7:10 am #8904Could you use this?
Monkey1234567891011121314151617181920212223242526#Import "<std>"Using std..Function Main()Local arr := New Int[][10]For Local i := 0 Until 10arr[i] = New Int[10]Nextarr[3][3] = 33arr[4][4] = 44arr[5][5] = 55Local arr2 := New Int[][10]arr.CopyTo(arr2,0,0,arr.Length)Print arr2[3][3]Print arr2[4][4]Print arr2[5][5]EndIf you mean multimensional [10,10] arrays, I don’t know.
June 25, 2017 at 7:29 am #8910Yeah, I was wondering how to do it with the [,] arrays. Maybe someone else knows.
edit: I guess if you would create a function that returns a multidimensional array and inputs a multimimensional array you would be able to copy it that way.
June 25, 2017 at 2:52 pm #8916[/crayon]Monkey1234567891011121314151617[crayon-5cb9d2d071d90371696888 inline="true" ]Function CopyMDArray<T>:T[,](Arr:T[,])Local x:=Arr.GetSize(0)Local y:=Arr.GetSize(1)Local newArr:=New T[x,y]For local i:=0 To x-1For Local j:=0 To y-1newArr[i,j]=Arr[i,j]NextNextReturn newArrEndJune 25, 2017 at 10:20 pm #8922There is no built-in way to do this yet (it’s easy for ‘2’ or ‘3’ dimensions, harder for ‘N’!) but abakobo’s solution look cool, although I’d probably rename it to Copy2DArray.
June 26, 2017 at 12:47 am #8924Returning arrays from functions do not require you to copy each element. This is done automatically. Only the Return is needed
Monkey1234Function copy2darray<T>:T[,](in:T[,])Return inEnd FunctionJune 26, 2017 at 1:09 am #8925I also tried to do this with the Lambda but I was not able fo figure out how to use the Typecasts <T>
The regular way works.
Monkey123456Local copy2darray := Lambda:Int[,](in:int[,])Return inEnd LambdaUsage :myarray2 = copy2darray(myarray1)Lambda’s are kind of like functions but they can be put near the code that you are working with.
June 26, 2017 at 1:10 am #8926@pakz:
Looks like this is not a real copy:Monkey12345678910111213141516171819202122232425#Import "<std>"Using std..Function Copy2DArray<T>:T[,](in:T[,])Return inEndFunction Main()Local arr1 := New Int[5,5]arr1[3,3] = 33Print arr1[3,3]Local arr2 := Copy2DArray(arr1)Print arr2[3,3]Print arr2[4,4]arr1[4,4] = 44 ' change arr1...Print arr2[3,3]Print arr2[4,4] ' ...arr2 is also changedEndJune 26, 2017 at 1:14 am #8927No, just checked it. You are right. Good to know.
btw, anyone know how to do the typecast with the lambda’s?
June 26, 2017 at 1:26 am #8928Arrays are not copied automatically, ie: they are reference types.
June 26, 2017 at 1:58 am #8930So @abakobo was right with his way of copying everything.
We can make this for 3 dimensions:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107'' http://monkeycoder.co.nz/forums/topic/multidimensional-arrays-and-copyto/'Function CloneArray<T>:T[](Arr:T[]) ' 1 dimensionLocal x := Arr.GetSize(0)Local newArr := New T[x]For local i:=0 Until xnewArr[i] = Arr[i]NextReturn newArrEndFunction CloneArray<T>:T[,](Arr:T[,]) ' 2 dimensionsLocal x := Arr.GetSize(0)Local y := Arr.GetSize(1)Local newArr := New T[x,y]For local i:=0 Until xFor Local j:=0 Until ynewArr[i,j]=Arr[i,j]NextNextReturn newArrEndFunction CloneArray<T>:T[,,](Arr:T[,,]) ' 3 dimensionsLocal x := Arr.GetSize(0)Local y := Arr.GetSize(1)Local z := Arr.GetSize(2)Local newArr:=New T[x,y,z]For local i:=0 Until xFor Local j:=0 Until yFor Local k:=0 Until znewArr[i,j,k]=Arr[i,j,k]NextNextNextReturn newArrEnd'===================================Function Main()Print "1 dimension:"Test1D()Print "2 dimensions:"Test2D()Print "3 dimensions:"Test3D()EndFunction Test1D()Local arr1D := New String[]( "a", "b", "c" )Local copy1D := CloneArray(arr1D)copy1D[1] = "d"Print arr1D[0]Print arr1D[1]Print arr1D[2]Print "==="Print copy1D[0]Print copy1D[1]Print copy1D[2]EndFunction Test2D()Local arr2D := New Int[5,5]arr2D[3,3] = 33Local copy2D := CloneArray(arr2D)Print copy2D[3,3]Print copy2D[4,4]arr2D[4,4] = 44Print copy2D[3,3]Print copy2D[4,4]EndFunction Test3D()Local arr3D := New Float[3,3,3]arr3D[0,1,2] = 1.5Local copy3D := CloneArray(arr3D)Print arr3D[0,1,2]Print copy3D[0,1,2]copy3D[0,1,2] = 2.5Print arr3D[0,1,2]Print copy3D[0,1,2]EndJune 26, 2017 at 2:29 am #8931This was easy enough in Original Monkey, and I would imagine that a variation could be used in Monkey 2. The trick was that if you already made a templated 1D array copy, you can then use it in your 2D one, and so on. OFC you still need to make separate ones for as many dimensions as you think you’ll need, but not much coding is involved. Here is the Monkey X version:
Class for generic functions
Monkey123456789101112131415161718192021222324252627282930313233343536' Class for generic functionsClass Generic< T >' Return a copy of the passed 1D array (will not 'deep copy' objects, just copies references)Function CopyArray:T[]( orig:T[] )Local len:Int = orig.Length()Local arr:T[] = New T[ len ]For Local i:Int = 0 Until lenarr[ i ] = orig[ i ]NextReturn arrEnd' Return a copy of the passed 2D array (will not 'deep copy' objects, just copies references)Function CopyArray:T[][]( orig:T[][] )Local len:Int = orig.Length()Local arr:T[][] = New T[ len ][]For Local i:Int = 0 Until lenarr[ i ] = CopyArray( orig[ i ] )NextReturn arrEnd' Return a copy of the passed 3D array (will not 'deep copy' objects, just copies references)Function CopyArray:T[][][]( orig:T[][][] )Local len:Int = orig.Length()Local arr:T[][][] = New T[ len ][][]For Local i:Int = 0 Until lenarr[ i ] = CopyArray( orig[ i ] )NextReturn arrEndEnd -
AuthorPosts
You must be logged in to reply to this topic.