Jag har en array som jag fyller med ett recordset.
dim strSQL,rs,thevek,getNextCompId
strSQL = "SELECT tCompetenceTypeId FROM viewCompetence WHERE tUserId = " & theUserId & " ORDER BY sGroupName, sName"
call getRsWithSQL(rs,theDb,strSQL)
thevek = rs.GetRows()
if ubound(thevek,2) >= (currentIndex) then
getNextCompId = thevek(0,currentIndex)
else
getNextCompId = 0
end if
Nu är det vidare så att i vissa fall har jag värdet och måste plocka ut vilket index det har i min array/vektor, så att jag vet vilken plats värdet har i array'en.
ASP/VbScript är tyvärr väldigt klent vad gäller array-funktioner.
Men här är en funktion som jag skrivit som söker efter ett värde i en array, och returnerar index om värdet finns, och false om värdet inte finns.
Den fungerar alltså likt funktionen array_search i PHP.
Function ArraySearch(needle, haystack)
dim i, index
index = false
If isArray(haystack) Then
For i = lBound(haystack) to uBound(haystack)
If haystack(i) = needle Then
index = i
Exit For
End if
Next
End if
ArraySearch = index
End Function
Använd såhär:
dim test
test = array("Tjabba", "Tjena", "Hallå")
response.write ArraySearch("Tjena", test) 'Detta skriver ut 1
Hmmm, tack för hjälpen, men den verkar inte vilja jämföra i den innersta If-satsen (if haystack(i) = needle then...) om vektorn har mer än 2 dimensioner. Går inte in i den alls.
Function ArraySearch(theId, theArr)
dim i, index
index = false
If isArray(theArr) Then
For i = lBound(theArr,2) to uBound(theArr,2)
If theArr(0,i) = theId Then
index = i
Exit For
End if
Next
End if
ArraySearch = index
End Function
Jag löste det genom att sätta int runt värdena jag jämför.
Tack till Troxy för hjälp på traven!
<%Function ArraySearch(theId, theArr)
dim i, index, num
index = false
If isArray(theArr) Then
For i = lBound(theArr,2) to uBound(theArr,2)
num = theArr(0,i)
If int(theid) = int(num) Then
index = i
Exit For
End if
Next
End if
ArraySearch = index
End Function%>