views:

660

answers:

3

How can I find out the number of dimensions in an array in Classic ASP ( VBScript ) .

I am being passed an Array with multiple dimensions but I only want to look at the last. Seems easy in other languages.

+4  A: 
Ubound(MySingleDimensionalArray, 2) ' Number of Array Elements

Ubound(MyMultiDimensionalArray, 1)  ' Number of Columns
Ubound(MyMultiDimensionalArray, 2)  ' Number of Rows
Bullines
If MySingleDimensionalArray is a single dimensional array then your first line will error.
jammus
A: 
function ArrayDimensions( theArray )
    dim Result,test
    Result = 0
    if isarray(theArray) then
     on error resume next
      do
       test = -2
       test = ubound(theArray,result+1)
       if test > -2 then result = result + 1
      loop until test=-2
     on error goto 0
    end if
    ArrayDimensions = Result
end function
feihtthief
A: 

Similar approach to feihtthief's answer here as I assume this is what you want rather than the size of a specified dimension.

Function NumDimensions(arr)
    Dim dimensions : dimensions = 0
    On Error Resume Next
    Do While Err.number = 0
        dimensions = dimensions + 1
        UBound arr, dimensions
    Loop
    On Error Goto 0
    NumDimensions = dimensions - 1
End Function

Then calling it as so:

Dim test(9, 5, 4, 3, 9, 1, 3, 5)
NumDimensions(test)

will give you the value 8

It's a bit crappy but it'll do what you asked.

jammus