Decker87 Posted December 25, 2008 Posted December 25, 2008 Hi. I am trying to write some UDFs (for myself and the community) that deal with arrays of arbitrary dimensions. This is often quite difficult because it means I can't use syntax such as $arr[2][2] if the array passed has three dimensions. This is where I am looking for a secondary was to reference an array, similar to C++. As many of you know, in C++ an array is simply a pointer to a memory location, and it's dimensions are simply offsets within that memory location dedicated to the array. For example, say I have an array that is defined like so: Dim $arr[4][3] Now, say I want to reference the last element in this array: $arr[3][2] What I'm looking for is a more primitive way of referencing this element, something like $arr + 12. Obviously this isn't the correct syntax, but I am trying to convey the idea. It would make it _much_ easier to write functions that deal with many-dimensional arrays easier.
Valuater Posted December 25, 2008 Posted December 25, 2008 Maybe... Dim $myArray[10][20] ;element 0,0 to 9,19 $rows = UBound($myArray) $cols = UBound($myArray, 2) $dims = UBound($myArray, 0) MsgBox(0, "The " & $dims & "-dimensional array has", _ $rows & " rows, " & $cols & " columns") 8)
Malkey Posted December 25, 2008 Posted December 25, 2008 (edited) Hi. I am trying to write some UDFs (for myself and the community) that deal with arrays of arbitrary dimensions. This is often quite difficult because it means I can't use syntax such as $arr[2][2] if the array passed has three dimensions. This is where I am looking for a secondary was to reference an array, similar to C++. As many of you know, in C++ an array is simply a pointer to a memory location, and it's dimensions are simply offsets within that memory location dedicated to the array. For example, say I have an array that is defined like so: Dim $arr[4][3] Now, say I want to reference the last element in this array: $arr[3][2] What I'm looking for is a more primitive way of referencing this element, something like $arr + 12. Obviously this isn't the correct syntax, but I am trying to convey the idea. It would make it _much_ easier to write functions that deal with many-dimensional arrays easier.Try this example of the PrimRef() function - (Primitive Reference). expandcollapse popup#include <array.au3> Dim $arr[4][3] = [[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]] MsgBox(0, "2 Dimensional Example","PrimRef($arr, 12) = " & PrimRef($arr, 12)) ;_ArrayDisplay($arr) ;Primative reference to an array Func PrimRef(ByRef $array, $Ref) If ($Ref > (UBound($array) * UBound($array, 2))) Or $Ref <= 0 Then Return "subscript dimension range exceeded" Return $array[Int(($Ref - 1) / (UBound($array) - 1))][Mod($Ref - 1, UBound($array) - 1)] EndFunc ;==>PrimRef ;======= 3 Dimensional array example =========================== Dim $arr[2][4][3] = [[[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]], _ [[13, 14, 15],[16, 17, 18],[19, 20, 21],[22, 23, 24]]] ConsoleWrite(" UBound($arr) " & UBound($arr) & " Default = 1 See UBound($arr,1)" & @CRLF) ConsoleWrite(" UBound($arr,0) " & UBound($arr, 0) & " Number of dimensions in array" & @CRLF) ConsoleWrite(" UBound($arr,1) " & UBound($arr, 1) & " Size of 1st dimension" & @CRLF) ConsoleWrite(" UBound($arr,2) " & UBound($arr, 2) & " Size of 2nd dimension" & @CRLF) ConsoleWrite(" UBound($arr,3) " & UBound($arr, 3) & " Size of 3rd dimension" & @CRLF) MsgBox(0, " 3 Dimensional Example"," _ArrayRef3Dim($arr, 24)) = " & _ArrayRef3Dim($arr, 24)) ;Primative reference to an array Func _ArrayRef3Dim(ByRef $array, $Ref) If ($Ref > (UBound($array, 1) * UBound($array, 2) * UBound($array, 3))) Or $Ref <= 0 Then Return "subscript dimension range exceeded" Local $Dim1 = Int(($Ref - 1) / ((UBound($array, 2)) * (UBound($array, 3)))) Local $Dim2 = Int(($Ref - 1 - $Dim1 * (UBound($array, 2)) * (UBound($array, 3))) / ((UBound($array, 3)))) Local $Dim3 = $Ref - 1 - (($Dim1 * UBound($array, 2) * $Dim1 * UBound($array, 3)) + $Dim2 * (UBound($array, 3))) Return $array[$Dim1][$Dim2][$Dim3] EndFunc ;==>PrimRef3Dim ;=======> End of 3 Dimensional array example ====================== Edit: Added Example to reference a three dimensional array. Edited December 25, 2008 by Malkey
Decker87 Posted December 25, 2008 Author Posted December 25, 2008 Hmm, that is a nice function, but it doesn't solve the problem. I am trying to find a way of dealing with arrays of arbitrary dimensions - what you wrote there only works for two or three dimensions. If I were to pass to those functions a 27-dimension array, I'd still get an error. This is the fundamental problem I am trying to solve.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now