What is the meaning of is
and in
in VBScript? Because they appear in blue when I write them in UFT.
Can I use them to check whether a value exists in matrix without looping? Because that's actually what I'm looking for. I mean something like this:
If "Ahmed" is in Matrix() Then
msgbox "He's in"
End If
When in doubt, read the documentation. The Is
operator is used to check if two object references (object variables) point to the same object:
Set sh1 = CreateObject("WScript.Shell")
Set sh2 = CreateObject("WScript.Shell")
Set sh3 = sh1
WScript.Echo "" & (sh1 Is sh2) 'Output: False
WScript.Echo "" & (sh1 Is sh3) 'Output: True
The keyword In
kind of exists in VBScript, but only as part of a For Each
loop:
Set fso = CreateObject("Scripting.FileSystemObject")
'"In" used in For Each loop.
For Each f In fso.GetFolder("C:\some\folder").Files
WScript.Echo f.Name
Next
You could use a For Each
loop for checking if an array (one array dimension to be precise) contains a specific value:
For Each element In arr
If element = refElement Then
...
End If
Next
I'm not aware of a way to check if a VBScript array contains a specific element without a loop, though (unless you count joining the elements to a string and then testing for an element with InStr
, which doesn't work for all data types). You could do that with a Dictionary
:
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", True
d.Add "b", True
WScript.Echo "" & d.Exists("a") 'Output: True
WScript.Echo "" & d.Exists("c") 'Output: False
or an ArrayList
:
Set al = CreateObject("System.Collections.ArrayList")
al.Add "a", True
al.Add "b", True
WScript.Echo "" & al.Contains("a") 'Output: True
WScript.Echo "" & al.Contains("c") 'Output: False
However, using different data structures would most likely require changes to the rest of your code.