Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get index of an element in an array using VBScript?

6,138 views
Skip to first unread message

Vasanthi

unread,
Feb 11, 2005, 3:09:02 PM2/11/05
to
Hi All,

I have an array and would like to find out the index of an element in that
when I know the actual value of the element.

I need to do it in VBScript, not in VB.

For example,
Lets assume that I have an array like follwing.

Dim MyArray1 (3)

MyArray1(0) = "Sunday"
MyArray1(1) = "Monday"
MyArray1(2) = "Tuesday"

Is there any method/fucntion/property that takes either 'Monday' as input
parameter and returns '1' ?

In another way, is there any equivalent method in VBScript similar to
'indexOf' method in Java Script??

Any help would be greatly appreciated.

Thanks,
Vasanthi


Veign

unread,
Feb 11, 2005, 7:04:56 PM2/11/05
to
Did you try posting in the VBScript newsgroup?

Also, you will have to loop through the array looking for a match..

Like (VB way):
Private Function IndexOf(Ary As Variant, strToMatch As String) As Long

'Default to an error code
IndexOf = -1

'Make sure an array was passed
If Not IsArray(Ary) Then Exit Function

'Check for matching index
Dim i As Long
For i = 0 To UBound(Ary)
If StrComp(strToMatch, Ary(i), vbTextCompare) = 0 Then
IndexOf = i
Exit For
End If
Next

End Function

VBScript way:
Private Function IndexOf(Ary, strToMatch)

'Default to an error code
IndexOf = -1

'Make sure an array was passed
If Not IsArray(Ary) Then Exit Function

'Check for matching index
Dim i As Long
For i = 0 To UBound(Ary)
If StrComp(strToMatch, Ary(i), vbTextCompare) = 0 Then
IndexOf = i
Exit For
End If
Next

End Function

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--

"Vasanthi" <Vasa...@discussions.microsoft.com> wrote in message
news:BC0796AF-D56B-4382...@microsoft.com...

Vasanthi

unread,
Feb 11, 2005, 9:23:05 PM2/11/05
to
Hi Veign,

Thanks for your reply.
I really don't want to loop through the array. I am intrested in using some
direct function/property.
Any ideas??

Thanks,
Vasanthi

Veign

unread,
Feb 11, 2005, 10:18:11 PM2/11/05
to
What you want and what you can actually accomplish may be two different
things. You may be able to come up with some sort of Split / Join hack but
I could not think of anything..

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--

"Vasanthi" <Vasa...@discussions.microsoft.com> wrote in message

news:585DD5AE-5A8C-44DA...@microsoft.com...

squadjot

unread,
Aug 3, 2011, 11:01:05 AM8/3/11
to
This is how to do it:

myTxt = "Hello World"
wscript.echo InStrRev(myTxt,"o")
wscript.echo InStrRev(myTxt,"l")
wscript.echo InStrRev(myTxt,"d")
wscript.echo InStrRev(myTxt,"H")
0 new messages