Connecting Tech Pros Worldwide Help | Site Map

Array Index / Item position ?

  #1  
Old November 14th, 2006, 08:35 PM
midlothian@gmail.com
Guest
 
Posts: n/a
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

  #2  
Old November 14th, 2006, 09:15 PM
Rich P
Guest
 
Posts: n/a

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 ***
  #3  
Old November 14th, 2006, 09:25 PM
Terry Kreft
Guest
 
Posts: n/a

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 ***

  #4  
Old November 15th, 2006, 12:25 AM
Lyle Fairfield
Guest
 
Posts: n/a

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?

  #5  
Old November 15th, 2006, 12:35 AM
Tom van Stiphout
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templated array of templates specialized by array index npankey@gmail.com answers 6 October 12th, 2008 04:25 PM
array optimizer =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= answers 7 August 28th, 2008 03:15 AM
create a dynamic array of pointers with initial values of NULL sandy@murdocks.on.ca answers 23 December 1st, 2006 11:05 PM
IList items "keeping position" Giovanni Bassi answers 3 November 20th, 2005 03:36 AM