May 30, 2020, 03:07:30 AM

Author Topic: [M2] Resize 2 dimensional arrays  (Read 1065 times)

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 483
[M2] Resize 2 dimensional arrays
« on: July 18, 2017, 01:30:22 PM »
This function will resize a 2 dimensional array while keeping the contents.  Shouldn't be too difficult to modify for arrays of higher dimensions.
Code: [Select]
Namespace myapp

#Import "<std>"

Using std..

Function resize2<T>:T[,](source:T[,],x:Int,y:Int)
Local dest := New T[x,y]

For Local i:Int = 0 Until Min(source.GetSize(0),dest.GetSize(0))
For Local j:Int = 0 Until Min(source.GetSize(1),dest.GetSize(1))
dest[i,j] = source[i,j]
Next
Next
Return dest
end

Function Main()

'create array and fill with random data
Local test:String[,] = New String[5,5]
For Local x:Int = 0 To 4
For Local y:Int = 0 To 4
test[x,y] = String.FromChar(Rnd(26)+65)
Next
next


printArray(test)

'increase array size
test = resize2(test,10,10)

printArray(test)

'decrease array size
test = resize2(test,7,7)

printArray(test)

'increase one dimention, decrease another
test = resize2(test,10,3)

printArray(test)

End

Function printArray(source:String[,])
Print "-----------------------------------------~n"
For Local y:Int = 0 Until source.GetSize(1)
Local s:String = ""
For Local x:Int = 0 Until source.GetSize(0)
If Not source[x,y]
s += "# "
Else
s += source[x,y] + " "
Endif
Next
Print s
Next
Print "~n----------------------------------------"
end
------------------------------------------------
8 rabbits equals 1 rabbyte.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal