Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
如何检查在vbscript中的数组中的变量 - 源码之家

如何检查在vbscript中的数组中的变量

问题描述:

我正在重构我的代码并试图减少重复。我有这个工作代码如何检查在vbscript中的数组中的变量

<% If tree <> "" or (info <> "" and info <> "links" and info <> "privacy" and info <> "talks") Then %> 
      write stuff 
<% End If %> 

我把信息变量到一个数组

Dim info(3) 
info(0) = "Talks" 
info(1) = "Privacy" 
info(2) = "Links" 

我不清楚通过阵列

<% If tree <> "" or (info <> "" and **info <> arrayInfo** Then %> 
      write stuff 
<% End If %> 

一点帮助迭代。谢谢。

,如果你想使用一个表达(.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 

另一种技术是创建一个字符串和INSTR()。

InStr函数([开始,]字符串1,字符串[,比较]) 如果在字符串1中没有找到字符串2然后InStr返回0。

注意,管分离器是在两端的第一和最终位置重要我们搜索的字符串以及我们想要匹配的内容。否则你会得到误报。

<% 
dim sText 
sText="|Talks|Privacy|Links|" 

    If tree <> "" or (len(info) > 0 and instr(1, sText, "|" info & "|")) Then %> 
      write stuff 
<% End If %> 

该技术是值得与一些字符串测试。默认比较模式区分大小写,但可以使其不敏感。

查看http://www.w3schools.com/asp/func_instr.asp了解详情。

它比使用字典更纯粹,但值得注意。

虽然我同意上述使用Instr函数的答案,但还有一个选择。你的问题是问如何遍历数组来测试值。使用For..Next循环。下面的代码示例。

dim arrInfo(2) 
dim blnInfoGood 

blnInfoGood = true 

arrInfo(0) = "Talks" 
arrInfo(1) = "Privacy" 
arrInfo(2) = "Links" 

for k = lbound(arrInfo) to ubound(arrInfo) 
    if info = arrInfo(k) then 
     blnInfoGood = false 
     exit for 
    end if 
next 

if tree <> "" or blnInfoGood then 
    ' Write stuff 
end if 

希望这会有所帮助。

使用字典并使用更简单的条件。

<% 
set obj = server.createObject("scripting.dictionary") 

obj.add "links", "links" 
obj.add "privacy", "privacy" 
obj.add "talks", "talks" 

if tree <> "" and obj.exists(info)=false then 
    'write stuff 
end if 

set obj = nothing 
%> 

这是通过数组迭代最简单的方法,因为您具体询问了这个问题。

Dim info(3) 
    info(0) = "Talks" 
    info(1) = "Privacy" 
    info(2) = "Links" 

    for i = 0 to 2 
     if tree = info(i) then 
      'do stuff here with match 
     end if 
    next 
+0

@ StackHound25的答案的错误版本(错误UBOUND,没有退出) –

+0

@ Ekkehard.Horner Stackhound的回答更简单的版本,而不是错误的UBOUND。该数组将触及下标0,1和2.虽然UBound值实际上可能是3,但这不是循环写入的方式。我特意在数组大小已知并且是固定的时候不使用UBound,我猜是个人偏好。退出属于“在这里做东西”。我认为这很明显。为什么这么多人如此快速地在这里冷静下来? – Daniel

+0

因为很多人发布低质量的代码,并为他们的失误找到跛脚的借口('个人偏好','退出'可能属于'在这里做比赛')? –