How many dimensions in my array or get the last one

余生颓废 提交于 2019-12-14 03:49:44

问题


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


来源:https://stackoverflow.com/questions/273410/how-many-dimensions-in-my-array-or-get-the-last-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!