Connecting Tech Pros Worldwide Forums | Help | Site Map

Array Index / Item position ?

midlothian@gmail.com
Guest
 
Posts: n/a
#1: Nov 14 '06
Hello
Is there a way to get the index of an item in an array? For instance,
if I have:

MyArray = Array("NY","CT","TX","NM")

....can I see what position CT is in the array?
Thanks


Rich P
Guest
 
Posts: n/a
#2: Nov 14 '06

re: Array Index / Item position ?


THe only way to get an index from an array is to loop through the array
until you match the desired value. YOu can use a collection object
which uses keys collObj.Add(value, key).

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Terry Kreft
Guest
 
Posts: n/a
#3: Nov 14 '06

re: Array Index / Item position ?


.... but even with a collection you can't find the index of an item without
looping through the collection.

--

Terry Kreft


"Rich P" <rpng123@aol.comwrote in message
news:455a36c1$0$10300$815e3792@news.qwest.net...
Quote:
THe only way to get an index from an array is to loop through the array
until you match the desired value. YOu can use a collection object
which uses keys collObj.Add(value, key).
>
Rich
>
*** Sent via Developersdex http://www.developersdex.com ***

Lyle Fairfield
Guest
 
Posts: n/a
#4: Nov 15 '06

re: Array Index / Item position ?


midlothian@gmail.com wrote:
Quote:
Hello
Is there a way to get the index of an item in an array? For instance,
if I have:
>
MyArray = Array("NY","CT","TX","NM")
>
...can I see what position CT is in the array?
Thanks
If it were important I'd hack something up along these lines (it would
need some more work):

Dim MyArray As Variant
Dim tempString As String
MyArray = Array("NY", "CT", "TX", "NM")
tempString = "/" & Join(MyArray, "/") & "/"
Debug.Print UBound(Split(left(tempString, InStr(tempString, "/CT/")),
"/")) - 1

but ... why would it be important?

Tom van Stiphout
Guest
 
Posts: n/a
#5: Nov 15 '06

re: Array Index / Item position ?


On 14 Nov 2006 12:51:56 -0800, midlothian@gmail.com wrote:

How did you get these values into this variant array? Perhaps
something can be done at the source.
If not, just loop using a For Each loop. On small arrays like this the
ugly n/2 performance is negligible.

-Tom.

Quote:
>Hello
>Is there a way to get the index of an item in an array? For instance,
>if I have:
>
>MyArray = Array("NY","CT","TX","NM")
>
>...can I see what position CT is in the array?
>Thanks
Closed Thread