All Forums >> [Scripting] >> Post a VBScript >> InArray() Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
that's true, but Arrays don't. That's why I think, TNO's InArray() function is a valuable VBScript addon. Maybe the value could be increased with little effort by keeping the information about the position of the item found.
Function InArray(item,A) Dim i For i=0 To UBound(A) Step 1 If A(i) = item Then InArray=i Exit Function End If Next InArray=-1 End Function
Dim MyArray MyArray=Array("a","b","c","d","e","f","g","h","i","j")
Yeah, I agree ehvbs. I was just mentioning that Dictionary objects provide almost the same functionality. The difference being that while the exists function for Dictionaries is FAR quicker than iterating through an entire array, the dictionary also requires each key to be unique, so in a list where there might be valid duplicates, dictionaries can not be used - which is where the array search function comes in.
And while I agree that array searching is valuable, i just wish there were a more... efficient method for it than iterating through the entire array.
_____________________________
"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
Well, the base issue is the fact that vbscript does not inherently support associative arrays so your only choice is to use the Dictionary object for the associative arrays or iterate through the normal array.
The only possible way you could make this type of array more efficient is if you knew what index the value is at, then you could catch an error if it did not exist. But of course that would destroy the purpose of testing an entire array for that value alone (making it extremely inneficient.).
This is one of those issues where I prefer to use JScript. Using the Dictionary object is such an effort in comparison especially if you want to treat things like object.property or object.method()
Of course I guess you could use Class for this sort of thing, so maybe I'm just lazy
<another thought>
You could use Array.Join(",") then use InStr("blah") but depending on the case it may be less efficient and give you false positives.
You could use Array.Join(",") then use InStr("blah") but depending on the case it may be less efficient and give you false positives. "
This is what I thought to do but in what ways is it inefficient? I was also thinking a concatenated string might not hold enough memory at some point. Is this correct? It seems faster than iterating and the Dictionary object Exists() only works for keys.
< Message edited by richcrook -- 11/2/2007 3:27:35 AM >
richcrook, That is the exact method that I use myself! haha Here's my function:
It works against either a string or an array, and I've tested it with an array created with 166,000+ elements, created from a text file 11 MB in size. It takes on average of a tenth of a second to run against either the 11MB string, or the array created by Split()ing the string. Of course, it is case insensitive right now, but it suits my purposes to have it be such.
_____________________________
"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
This is what I thought to do but in what ways is it inefficient?
The inefficiencies depend on the structure of your array really and how its being used. Depending on structure it may not be as simple of a case as InStr("blah") but instead some type of InStr(some function result) that gets called repetitively.