how do i determine position an element in an array

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 13 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
biggorron
Posts: 4
Last visit: Thu Oct 07, 2010 10:17 am

how do i determine position an element in an array

Post by biggorron »

hi how do i determine the position an element exists in an array.
my array is called arryList. as an example my arryList looks like this

red
blue
green
yellow
END
purple
orange
white
bloack

if we look at END we can see it's in the 5th position but how can i find that out. thx for thelp in advance.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how do i determine position an element in an array

Post by jvierra »

Function FindArrayPosition( a, element )
for i = 1 to UBound(a)
if a(i - 1) = element Then
FindArrayPosition= i
Exit Function
end if
next
End Function

' if result is zero then element not found.
WScript.Echo FindArrayPosition( myarray, "END")
jvierra2010-10-07 12:28:34
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how do i determine position an element in an array

Post by jvierra »

This may make more senseL

Code: Select all

	myarray = Array("red","blue","green","yellow","END","purple","orange","white","bloack")
	' if result is zero then element not found.MsgBox FindArrayPosition( myarray, "END")
	Function FindArrayPosition( a, element )     for i = 1 to UBound(a)         if a(i-1) = element Then             FindArrayPosition = i             Exit Function         End if     nextEnd Function
	
User avatar
biggorron
Posts: 4
Last visit: Thu Oct 07, 2010 10:17 am

how do i determine position an element in an array

Post by biggorron »

ok i have tried both if i just copy your 2nd code there as is it works fine but hwen i add it and change it for my array that i am using i get compilation error: syntaxe error.

heres my code including what you gave me. oh and i forgot to mention there will be more then one "END" in the array.

Code: Select all

	
const ForReading = 1
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:new folderkocfirst.txt", ForReading)
	
 
	
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrList = Split(strNextLine , " ")
	
' if result is zero then element not found.
MsgBox FindArrayPosition( ArrList, "END")
	
Function FindArrayPosition( arrList, element ) 
    for i = 1 to UBound(arrList)
         if arrList(i-1) = element Then
             FindArrayPosition = i
             Exit Function
         End if 
    next
End Function
	
loop
User avatar
biggorron
Posts: 4
Last visit: Thu Oct 07, 2010 10:17 am

how do i determine position an element in an array

Post by biggorron »

I have done scripting before however i have not used vbscript before and jsut started learning it aout 5 days ago. i do understand now why you don't put the function code in the loop.
User avatar
jwestan
Posts: 48
Last visit: Mon May 04, 2015 5:59 am

how do i determine position an element in an array

Post by jwestan »

jvierra,

How could I find the middle number of the array if I had an array like:

LINE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,36,,,,,,,,,,,,,,,,,,181
I am able to find the last number with UBound like in the example you gave but I having issues find the middle number. It is never in the same array postion and the number changes from a 2 digit to a 3 digit number.
I am reading a line and replacing the spaces with commas to create the array above.
LINE 36 181


Thanks for any suggestions.
User avatar
jwestan
Posts: 48
Last visit: Mon May 04, 2015 5:59 am

how do i determine position an element in an array

Post by jwestan »

This is for Powershell correct?


I am using VBScript.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how do i determine position an element in an array

Post by jvierra »

Code: Select all

	
s="LINE                               36                  181"
While  InStr(s,"  ")
   s = Replace(s, "  "," ")
Wend
a = Split(s," ")
MsgBox a(1)
	
This topic is 13 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked