How do you check a value exists in an array?

D

Dooza

Using ASP with VBScript is there a clever way to easily check if a value
exists in an array without looping through it?

Steve
 
D

Dooza

McKirahan said:
If it's a one dimensional array you could try:

Const cVAL = "your_value"
Dim strVAL
strVAL = vbTab & cVAL & vbTab
Dim strARR
strARR = vbTab & Join(your_array,vbTab) & vbTab

If InStr(strARR,strVAL) > 0 Then
WScript.Echo "'" & cVAL & "' found."
Else
WScript.Echo "'" & cVAL & "' not found."
End If

I am using ADSI on our intranet, and only want to show certain users
certain menu items.

What I have done is this:

<%
Dim groupIT
Dim groupSALES
Dim groupACCOUNTS
Dim groupADMIN
For Each objGroup In ObjUser.Groups
If objGroup.Name = "IT" Then groupIT = 1
If objGroup.Name = "UK Sales" Then groupSALES = 1
If objGroup.Name = "Accounts" Then groupACCOUNTS = 1
If objGroup.Name = "Domain Admins" Then groupADMIN = 1
Next
%>

This is part of a larger file that gets the users details from the
Active Directory. This file is included on all the pages on the intranet
(or will be if my tests are all good)

I then use the variables declared above to conditionally display the
menus that they are allowed to see.

I was going to use the array on each section, but I thought that was a
bad idea due to the repetition of the loops. This way I loop once per
page load, and then display based on the variables that are set.

My tests show that it works. What does the group feel about this? I am
missing anything that I should take be aware of?

Here is the include file that I am using, just in case you asked:

<%
Dim strDomain
Dim strUser
Dim strAuth
Dim strUserFullName
Dim strDisabled
Dim strGroupMember
Dim strDomainAdmin
Dim strAuthUser
' Get logged in user to get domain and user details
If Request.ServerVariables("AUTH_USER") <> "" Then
strAuthUser = Request.ServerVariables("AUTH_USER")
strDomain = Left(strAuthUser,9)
strUser = Mid(strAuthUser,11,100)
Else
Response.Redirect("/accessdenied.asp")
End If
Dim objGroup
Dim objAdmin
Dim objUser
Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")
Set objAdmin = GetObject("WinNT://" & strDomain & "/Domain Admins,group")
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")

strDomainAdmin = objAdmin.IsMember(objUser.ADsPath)
strGroupMember = objGroup.IsMember(objUser.ADsPath)
strUserFullName = objUser.Fullname
strDisabled = objUser.AccountDisabled

' If member of the required group and account is not disabled allow access
' If member of Domain Admins allow access
If (strGroupMember AND NOT strDisabled) OR (strDomainAdmin) Then
strAuth = True
Else
strAuth = False
' Redirect if access is not permitted
Response.Redirect("/accessdenied.asp")
End If
%>
<%
Dim groupIT
Dim groupSALES
Dim groupACCOUNTS
Dim groupADMIN
For Each objGroup In ObjUser.Groups
If objGroup.Name = "IT" Then groupIT = 1
If objGroup.Name = "UK Sales" Then groupSALES = 1
If objGroup.Name = "Accounts" Then groupACCOUNTS = 1
If objGroup.Name = "Domain Admins" Then groupADMIN = 1
Next
%>
 
A

Anthony Jones

McKirahan said:
If it's a one dimensional array you could try:

Const cVAL = "your_value"
Dim strVAL
strVAL = vbTab & cVAL & vbTab
Dim strARR
strARR = vbTab & Join(your_array,vbTab) & vbTab

If InStr(strARR,strVAL) > 0 Then
WScript.Echo "'" & cVAL & "' found."
Else
WScript.Echo "'" & cVAL & "' not found."
End If

An interesting solution. Arguably that is two loops one peformed by the
join and the second by the instr but since both 'loops' in compiled code
that would well be faster than an basic For loop in VBScript but I doubt it,
I'll have to test that.
 
A

Anthony Jones

Dooza said:
Using ASP with VBScript is there a clever way to easily check if a value
exists in an array without looping through it?

Another alternative would be available if the contents of the array is
ordered. If so then a binary search would be much quicker on anything but a
very small array.

Another would be to not use an array but a Dictionary.
 
D

Dooza

Anthony said:
Another alternative would be available if the contents of the array is
ordered. If so then a binary search would be much quicker on anything but a
very small array.

Another would be to not use an array but a Dictionary.

I think I am restricted to whatever ADSI gives me, but I could be wrong.
See my code somewhere above.

Steve
 
B

Bob Barrows [MVP]

Anthony said:
An interesting solution. Arguably that is two loops one peformed by
the join and the second by the instr but since both 'loops' in
compiled code that would well be faster than an basic For loop in
VBScript but I doubt it, I'll have to test that.
I would have thought the impact of string conversion and concatentation
would be intolerable. To the OP: looping through an array is fast
(especially if the contents are sorted so unsuccessful searches can be
aborted early), so why look for an alternative?

 
B

Bob Barrows [MVP]

Dooza said:
I think I am restricted to whatever ADSI gives me, but I could be
wrong. See my code somewhere above.
? There is nothing restricting you to using an array vs. a Dictionary.
 
D

Dooza

Bob said:
? There is nothing restricting you to using an array vs. a Dictionary.

A dictionary allows me to use name/value pairs, is that right? I am only
needing the name of the group that the user is a member of, what would I
use as the pair?

Steve
 
B

Bob Barrows [MVP]

Dooza said:
A dictionary allows me to use name/value pairs, is that right? I am
only needing the name of the group that the user is a member of, what
would I use as the pair?
You could store an array of the group members, i.e
key: groupname
value: array of group members

I'm not saying this the correct way to do this: simply offering an
alternative.
 
A

Anthony Jones

Bob Barrows said:
I would have thought the impact of string conversion and concatentation
would be intolerable.

Join is somewhat more effecient at contentation but its a good point it
probably would be a killer.
 
A

Anthony Jones

Dooza said:
I am using ADSI on our intranet, and only want to show certain users
certain menu items.

What I have done is this:

<%
Dim groupIT
Dim groupSALES
Dim groupACCOUNTS
Dim groupADMIN
For Each objGroup In ObjUser.Groups
If objGroup.Name = "IT" Then groupIT = 1
If objGroup.Name = "UK Sales" Then groupSALES = 1
If objGroup.Name = "Accounts" Then groupACCOUNTS = 1
If objGroup.Name = "Domain Admins" Then groupADMIN = 1
Next

<snip>

Its doubtful that the set of groups for user would be large. You're also
testing for multple values in a single loop which makes good use of the
loop. I would say your done here move on.
 
D

Dooza

Bob said:
You could store an array of the group members, i.e
key: groupname
value: array of group members

I'm not saying this the correct way to do this: simply offering an
alternative.

Yes I see what your saying, I would then use something like this:

<% If group.Item("groupname") = groupIT Then %>

I would first need a loop to put the group names into the dictionary.

Now I understand more about the dictionary, I think in this case I won't
use it, but I do see its usefulness.

Cheers,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top