调整多维数组的大小并按日期对它们进行排序

时间:2012-06-01 13:03:54

标签: asp-classic

我认为我的代码成功创建了多维数组,因为当我使用UBound(DataArray)计算它时,我获得了正确的数量。

但是当我尝试将其中一个数据显示为Response.Write DataArray(1,0)时,我得到null值。

任何帮助表示赞赏!

 sDateArray = Split(DateArray, ",")
    sVenueArray = Split(VenueArray, ",")

    Dim DataArray()    
    For i = 0 to uBound(sDateArray)-1 
        ReDim DataArray(i, 1)
        DataArray(i, 0) = sDateArray(i)
        DataArray(i, 1) = sVenueArray(i)
    Next

    Response.Write UBound(DataArray) & "<br /><br />"
    DataArray(1,0)
    Response.Write DataArray(1,0)  

1 个答案:

答案 0 :(得分:1)

尝试使用Redim Preserve DataArray(i, 1)代替ReDim DataArray(i, 1)

...或...

sDateArray = Split(DateArray, ",")
sVenueArray = Split(VenueArray, ",")

Dim DataArray(uBound(sDateArray)-1, 1)    
For i = 0 to uBound(sDateArray)-1
    DataArray(i, 0) = sDateArray(i)
    DataArray(i, 1) = sVenueArray(i)
Next

Response.Write UBound(DataArray) & "<br /><br />"
' DataArray(1,0) ' <== commented out cos I think this might be an error - ?
Response.Write DataArray(1,0) 

好的,我很无聊所以我写了这个。

可能不完美 - 自从我使用经典ASP

以来已经有一段时间了
Function SortByDate(a_input)

    x = UBound(a_input, 1) - 1
    if( x < 1 ) Then
        Response.Write "<p>Invalid input array - first element is empty</p>"
        Stop
    End If
    Dim a_output(x, 1)
    Dim earliest_date

    For j=0 To x
        earliest_date = -1
        For i=0 To UBound(a_input, 1) - 1
            If a_input(0, i) <> "" Then
                If earliest_date = -1 Then
                    earliest_date = i
                Else
                    If CDate(a_input(0,i)) > CDate(a_input(0,earliest_date)) Then
                        earliest_date = i
                    End If
                 End If
            End If
        Next
        a_output(0, i) = a_input(0, earliest_date)
        a_output(1, i) = a_input(1, earliest_date)
        a_input(0, earliest_date) = "" ' this one is done so skip next time '
    Next

    SortByDate = a_output
End Function