Return Array
Posted: Sat Dec 17, 2016 4:09 pm
Is it possible to return an array from a procedure maybe by its pointer?
Code: Select all
EnableExplicit
Structure BuildArrayHelper
Array _array.s(1)
EndStructure
Procedure BuildArray()
Protected *Array.BuildArrayHelper
*Array = AllocateMemory(SizeOf(BuildArrayHelper))
InitializeStructure(*Array, BuildArrayHelper)
ReDim *Array\_array(6)
*Array\_array(0) = "Hello"
*Array\_array(1) = "I'am"
*Array\_array(2) = "an"
*Array\_array(3) = "Array"
*Array\_array(4) = "returned"
*Array\_array(5) = "from"
*Array\_array(6) = "Procedure"
ProcedureReturn *Array
EndProcedure
Define *Array.BuildArrayHelper = BuildArray(), x
For x = 0 To ArraySize(*Array\_array())
Debug *Array\_array(x)
Next
;free Memory if no longer needed
ClearStructure(*Array, BuildArrayHelper)
Code: Select all
Procedure InitData(Array data1(1))
Protected i
ReDim data1(10)
For i = 0 To 10
data1(i) = i + 1000
Next
ProcedureReturn ArraySize(data1())
EndProcedure
Global Dim MyData(0)
Debug InitData(MyData())
For i = 0 To 10
Debug MyData(i)
Next
And the same with Maps() and Lists() !mk-soft wrote:A clean method is to pass an array byref
Thanks Fred. I'm not quite sure how to do that but I'm trying.Fred wrote:You can allocate a memory buffer of the array size and copy all the elements in it
Code: Select all
Structure struct_array
a.i
b.s
EndStructure
Dim arr_struct.struct_array(9)
;: Fill Arrays
For i=0 To 9
arr_struct(i)\a = Random(100,1)
arr_struct(i)\b = Str(i)
Next i
Procedure GetOneStructArray(Array arr_struct.struct_array(1), Number.s = "3")
Protected i, Result = 0
For i=0 To 9
If arr_struct(i)\b = Number
Result = @arr_struct(i)
Break
EndIf
Next i
ProcedureReturn Result
EndProcedure
Original = @arr_struct(3)
fromProc = GetOneStructArray(arr_struct(), "3")
*Single.struct_array
*Single = fromProc
Debug Original
Debug fromProc
Debug "Result : "
Debug *Single\b