How to check if variable in array in vbscript

2019-07-21 21:07发布

问题:

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 %>

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 %>

Little help. Thanks.

回答1:

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

output:

cscript 42207316.vbs
Talks found
no Other in Talks Privacy Links
Links found
no Else in Talks Privacy Links


回答2:

Another technique is to create a string and instr().

InStr([start,]string1,string2[,compare]) If string2 is not found in string1 then InStr returns 0.

Note that the pipe separator is important at both the first and final positions of the string we search AND what we are seeking to match. Otherwise you get false-positives.

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

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

The technique is worthwhile with a few strings to test. The default compare mode is case sensitive but you can make it insensitive.

See http://www.w3schools.com/asp/func_instr.asp for details.

It is less purist than using a dictionary but worth being aware of.



回答3:

Although I agree with the above answer using the Instr function, there is an alternative. Your question is asking how to iterate through the array to test the values. Use a For..Next loop. Code example below.

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

Hope this helps.



回答4:

Use a dictionary AND use a simpler conditional.

<%
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
%>


回答5:

Here's the simplest way to iterate through the array since you asked specifically about that.

    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