Lee Farmer
Lee Farmer

Reputation: 13

VBScript array ? If then else

I am new to VBScript and a solution we utilise has VBScript to manipulate data

Doing if then else is not a problem but when I have a large data set, with little output I am looking to use some kind array, can I store the values in an array and still utilise if then else

Very basic in requirement but after looking around seem to be getting a little muddled up on syntax

Simple if then else which is fine, but would have to write a lot of if then else

    If Input1 = "A1" Then
    Output0 = "A"

    ElseIf Input1 = "B1" Then
    Output0 = "B"

    Else
    Output0 = "C"
    End If

What I need to achieve is, please note values aren't the same and a trim wouldn't be the requirement from the below

If Input1 = "A1,A2,A3" Then
Output0 = "A"

ElseIf Input1 = "B1,B2,B3" Then
Output0 = "B"

Else
Output0 = "C"
End If

Using SQL I would do an IN statement

Upvotes: 0

Views: 429

Answers (2)

For completeness, you can also use a Select Case statement, which allows an expression list in a Case eg

Select Case Input1
    Case "A1", "A2", "A3"
        Output0 = "A"
    Case "B1", "B2", "B3"
        Output0 = "B"
    Case "C1", "C2", "C3"
        Output0 = "C"
    Case Else
        Output0 = "D"
End Select

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

VBscript's tool to map inputs to outputs is the Dictionary. So use one as in:

>> Set d = CreateObject("Scripting.Dictionary")
>> d("A1") = "A"
>> d("A2") = "A"
>> d("B1") = "B"
>> d("C")  = "C"
>> For Each k In Split("C B1 A2 B2 A1")
>>     If d.Exists(k) Then
>>        WScript.Echo k, d(k)
>>     Else
>>        WScript.Echo k, "???"
>>     End If
>> Next
>>
C C
B1 B
A2 A
B2 ???
A1 A

Upvotes: 3

Related Questions