0

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

2
  • Have you looked at the Filter command, tutorialspoint.com/vbscript/vbscript_arrays.htm ? Commented Dec 7, 2015 at 16:53
  • @JBKing In terms of the example the OP has given Filter() would work but if the values varied not so much. For example AB1 would return Output0 = "A" and Output0 = "B". Commented Dec 7, 2015 at 17:20

2 Answers 2

3

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

3 Comments

Just out of interest, what do you use to generate your code samples?
My brain and a REPL based on visualbasicscript.com/…. Alternative: planetcobalt.net/sdb/vbsh.shtml
Thanks for suggestion, but this seems the same as doing a lot if then else statements, after a little more digging, I am going to take this kind of approach but I will build a sql table 1st then call that as an intermediate table for the data
0

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

2 Comments

Perfect other than ending with End Select not Case
Sorry about that, I should have tested it

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.