当我给它提供多个值时,我的数组比较函数可以正常工作。 但是,当两个数组仅包含一个值时,它将无法正常工作。
工作比较脚本:
<%
Function InArray(Needle, Haystack)
Dim i, x
InArray = False
If UBound(Haystack)>0 Then
For i = 0 To Ubound(Haystack)
If IsArray(Needle) = True Then
For x = 0 To Ubound(Needle)
If Trim(Haystack(i)) = Trim(Needle(x)) Then
InArray = True
Exit Function
End If
Next
Else
If Trim(Haystack(i)) = Trim(Needle) Then
InArray = True
Exit Function
End If
End If
Next
end if
End Function
Dim HRMarray: HRMarray=array(459,14,2345)
Dim ADSarray: ADSarray=array(459,123,14,3345)
For Each HRMemployee In HRMarray
If (inarray(HRMemployee,ADSarray) = False) then
response.write HRMemployee& " missing in ADSarray <br>"
end if
Next
For Each ADSemployee In ADSarray
If (inarray(ADSemployee,HRMarray) = False) then
response.write ADSemployee& " to much in ADSarray <br>"
end if
Next
%>
OK输出:
ADSarray中缺少2345个
ADSarray中的123到
3345到ADSarray中的
不起作用的比较脚本:
<%
Function InArray(Needle, Haystack)
Dim i, x
InArray = False
If UBound(Haystack)>0 Then
For i = 0 To Ubound(Haystack)
If IsArray(Needle) = True Then
For x = 0 To Ubound(Needle)
If Trim(Haystack(i)) = Trim(Needle(x)) Then
InArray = True
Exit Function
End If
Next
Else
If Trim(Haystack(i)) = Trim(Needle) Then
InArray = True
Exit Function
End If
End If
Next
end if
End Function
Dim HRMarray: HRMarray=array(459)
Dim ADSarray: ADSarray=array(459)
For Each HRMemployee In HRMarray
If (inarray(HRMemployee,ADSarray) = False) then
response.write HRMemployee& " missing in ADSarray <br>"
end if
Next
For Each ADSemployee In ADSarray
If (inarray(ADSemployee,HRMarray) = False) then
response.write ADSemployee& " to much in ADSarray <br>"
end if
Next
%>
NOK输出:
459在ADSarray中丢失
459到ADSarray中的数量
此输出不是oke,因为两个数组都包含相同的内容,因此它根本不应该提供任何输出。
我希望有人看到这里出了问题。
此致
弗兰斯
答案 0 :(得分:3)
省略If UBound(Haystack)>0 Then
行(以及相应的end if
),或将其更改为If UBound(Haystack) >= 0 Then
。
说明:UBound
函数对于一个元素数组返回0
,对于一个空元素(无元素数组)返回-1
。
使用以下测试脚本的证明:
option explicit
Dim HRMarray: HRMarray=array(459)
Dim ADSarray: ADSarray=array()
Dim strRes
strRes = _
UBound( HRMarray) & vbTab & varType( HRMarray) & vbTab & typeName( HRMarray) _
& vbNewLine _
& UBound( ADSarray) & vbTab & varType( ADSarray) & vbTab & typeName( ADSarray)
Wscript.Echo strRes
结果:
==> cscript .\SO\56611737a.vbs
0 8204 Variant() -1 8204 Variant()