Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


InArray()

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,41079
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> InArray()
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 InArray() - 12/16/2006 12:35:59 AM   
  TNO


Posts: 1036
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Heres a function I never found in VBScript, so I ended up creating it as a time saver in a variety of projects:

      

_____________________________

Consolidated Script Component: The Acid Test

A universe of complexity...
 
 
Post #: 1
 
 RE: InArray() - 12/17/2006 12:00:29 PM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
Dictionary objects have something like this.


      


_____________________________

"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)

(in reply to TNO)
 
 
Post #: 2
 
 RE: InArray() - 12/17/2006 6:40:01 PM   
  ehvbs

 

Posts: 2012
Score: 48
Joined: 6/22/2005
From: Germany
Status: offline
Hi DiGiTAL.SkReAM,

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")

Dim nIdx : nIdx = InArray( "d", MyArray )
MsgBox "Position: " & nIdx
MsgBox "Found: " + CStr( (-1 < nIdx) )

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 3
 
 RE: InArray() - 12/18/2006 5:44:51 PM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
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)

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: InArray() - 12/18/2006 8:10:54 PM   
  TNO


Posts: 1036
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
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.

_____________________________

Consolidated Script Component: The Acid Test

A universe of complexity...

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 5
 
 RE: InArray() - 12/18/2006 9:18:13 PM   
  ehvbs

 

Posts: 2012
Score: 48
Joined: 6/22/2005
From: Germany
Status: offline
How about

bIsIn = 0 < Instr( Join( aArray, vbTab ), sToSearchFor + vbTab )

replace vbTab with Chr(0) for special cases

(all aircode, I admit)

(in reply to TNO)
 
 
Post #: 6
 
 RE: InArray() - 11/1/2007 5:27:02 PM   
  richcrook

 

Posts: 1
Score: 0
Joined: 11/1/2007
Status: offline
TNO
 
"<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. "

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 >

(in reply to ehvbs)
 
 
Revisions: 1 | Post #: 7
 
 RE: InArray() - 11/2/2007 12:30:39 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
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)

(in reply to richcrook)
 
 
Post #: 8
 
 RE: InArray() - 11/5/2007 7:05:28 PM   
  TNO


Posts: 1036
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
quote:

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.

_____________________________

Consolidated Script Component: The Acid Test

A universe of complexity...

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 9
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> InArray() Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts