How many dimensions in my array or get the last on

2019-07-04 13:22发布

问题:

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.

回答1:

Ubound(MySingleDimensionalArray, 2) ' Number of Array Elements

Ubound(MyMultiDimensionalArray, 1)  ' Number of Columns
Ubound(MyMultiDimensionalArray, 2)  ' Number of Rows


回答2:

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.



回答3:

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