如何在vbscript中检查数组中的变量 [英] How to check if variable in array in vbscript

查看:130
本文介绍了如何在vbscript中检查数组中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构代码并尝试减少重复.我有此工作代码

I'm refactoring my code and trying to cut down on repetition. I've got this working code

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

我将info变量放入了一个数组

I put the info variables into an array

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

我不清楚是否要遍历数组

I'm unclear as to iterate through the array

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

几乎没有帮助.谢谢.

推荐答案

如果要使用一个表达式(.Exists)来获取有关<集合的strong>所有元素.看:

You need a dictionary if you want to use one expression (.Exists) to obtain a fact (contained or not) about all elements of a collection. Look at:

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

这篇关于如何在vbscript中检查数组中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆