3

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.

6 Answers 6

8

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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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.

Comments

2

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

Comments

-1

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

4 Comments

Bad version of @StackHound25's answer (wrong ubound, no exit)
@Ekkehard.Horner Simpler version of Stackhound's answer and not the wrong ubound. The array will touch subscripts 0, 1, and 2. While the UBound value might actually be 3 here, that's not how the loop would be written. I intentionally do not use UBound when the array size is known and fixed, personal preference I guess. And exit falls under "do stuff here". I thought that would be obvious. Why are so many people so quick to downvote on here?
Because many people post low-quality code and find lame excuses for their blunders ('personal preference', how can 'exit' possibly fall under 'do stuff here with match'?)
@Ekkehard.Horner Gimme a break, it's obvious you're just downvoting because you have an answer here. Exit most definitely falls under "do stuff here" because "Do stuff here" means do what you need to do with the matching value and exit is not necessarily what he may need to do here. Exit is not the only option so therefore I included it. Regardless, the answer works and is SIMPLE. That's what I was trying to provide the OP since no one else has.
-1

Heres a clean way:

Dim isIn
Dim opt
Dim ok

ok = false
isIn = "100"

opt=Split("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19")

for i = Lbound(opt) to Ubound(opt)
    if opt(i) = isIn then
        ok = true
    end if
next

MsgBox "Value found " & ok
Wscript.Quit

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.