作者:手机用户2502875691_190 | 来源:互联网 | 2023-02-12 07:22
我正在重构我的代码并试图减少重复.我有这个工作代码
<% If tree <> "" or (info <> "" and info <> "links" and info <> "privacy" and info <> "talks") Then %>
write stuff
<% End If %>
我把info变量放到一个数组中
Dim info(3)
info(0) = "Talks"
info(1) = "Privacy"
info(2) = "Links"
我不清楚迭代数组
<% If tree <> "" or (info <> "" and **info <> arrayInfo** Then %>
write stuff
<% End If %>
没什么帮助.谢谢.
1> Ekkehard.Hor..:
如果要使用一个表达式(.Exists)来获取有关集合的所有元素的事实(包含或不包含),则需要一个字典.看着:
Option Explicit
Dim aInfo(2) ' last index; not size
aInfo(0) = "Talks"
aInfo(1) = "Privacy"
aInfo(2) = "Links"
Dim dicInfo : Set dicInfo = CreateObject("Scripting.Dictionary")
dicInfo.CompareMode = vbTextCompare
Dim i
For Each i In aInfo
dicInfo(i) = 0
Next
For Each i In Split("Talks Other Links Else")
If dicInfo.Exists(i) Then
WScript.Echo i, "found"
Else
WScript.Echo "no", i, "in", Join(dicInfo.Keys())
End If
Next
输出:
cscript 42207316.vbs
Talks found
no Other in Talks Privacy Links
Links found
no Else in Talks Privacy Links