How do you check a value exists in an array?

How do you check a value exists in an array?

am 17.01.2008 16:45:51 von 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

Re: How do you check a value exists in an array?

am 17.01.2008 16:49:19 von reb01501

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

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: How do you check a value exists in an array?

am 17.01.2008 16:55:52 von Dooza

Bob Barrows [MVP] wrote:
> Dooza wrote:
>> Using ASP with VBScript is there a clever way to easily check if a
>> value exists in an array without looping through it?
>>
> No
>
I thought as much, just wanted to double check. Thanks Bob.

Steve

Re: How do you check a value exists in an array?

am 17.01.2008 17:02:24 von McKirahan

"Dooza" wrote in message
news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
> Using ASP with VBScript is there a clever way to easily check if a value
> exists in an array without looping through it?

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

Re: How do you check a value exists in an array?

am 17.01.2008 17:36:01 von Dooza

McKirahan wrote:
> "Dooza" wrote in message
> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
>> Using ASP with VBScript is there a clever way to easily check if a value
>> exists in an array without looping through it?
>
> 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
%>

Re: How do you check a value exists in an array?

am 17.01.2008 17:39:30 von Anthony Jones

"McKirahan" wrote in message
news:WoKdnTdwKNgv5hLanZ2dnUVZ_ramnZ2d@comcast.com...
> "Dooza" wrote in message
> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
> > Using ASP with VBScript is there a clever way to easily check if a value
> > exists in an array without looping through it?
>
> 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.


--
Anthony Jones - MVP ASP/ASP.NET

Re: How do you check a value exists in an array?

am 17.01.2008 17:43:07 von Anthony Jones

"Dooza" wrote in message
news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
> 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.

--
Anthony Jones - MVP ASP/ASP.NET

Re: How do you check a value exists in an array?

am 17.01.2008 17:48:54 von Dooza

Anthony Jones wrote:
> "Dooza" wrote in message
> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
>> 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.
>

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

Steve

Re: How do you check a value exists in an array?

am 17.01.2008 17:52:20 von reb01501

Anthony Jones wrote:
> "McKirahan" wrote in message
> news:WoKdnTdwKNgv5hLanZ2dnUVZ_ramnZ2d@comcast.com...
>> "Dooza" wrote in message
>> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
>>> Using ASP with VBScript is there a clever way to easily check if a
>>> value exists in an array without looping through it?
>>
>> 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.
>
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?


> --
> Anthony Jones - MVP ASP/ASP.NET

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: How do you check a value exists in an array?

am 17.01.2008 17:55:08 von reb01501

Dooza wrote:
> Anthony Jones wrote:
>> "Dooza" wrote in message
>> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
>>> 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.
>>
>
> 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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: How do you check a value exists in an array?

am 17.01.2008 18:08:27 von Dooza

Bob Barrows [MVP] wrote:
>>> 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.
>>
> ? 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

Re: How do you check a value exists in an array?

am 17.01.2008 19:18:49 von reb01501

Dooza wrote:
> Bob Barrows [MVP] wrote:
>>>> 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.
>>>
>> ? 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?
>
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: How do you check a value exists in an array?

am 17.01.2008 23:03:15 von Anthony Jones

"Bob Barrows [MVP]" wrote in message
news:uKAqUlSWIHA.5132@TK2MSFTNGP02.phx.gbl...
> Anthony Jones wrote:
> > "McKirahan" wrote in message
> > news:WoKdnTdwKNgv5hLanZ2dnUVZ_ramnZ2d@comcast.com...
> >> "Dooza" wrote in message
> >> news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
> >>> Using ASP with VBScript is there a clever way to easily check if a
> >>> value exists in an array without looping through it?
> >>
> >> 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.
> >
> 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.

--
Anthony Jones - MVP ASP/ASP.NET

Re: How do you check a value exists in an array?

am 17.01.2008 23:07:05 von Anthony Jones

"Dooza" wrote in message
news:e6KWnbSWIHA.4440@TK2MSFTNGP06.phx.gbl...
> McKirahan wrote:
> > "Dooza" wrote in message
> > news:OILNg$RWIHA.5340@TK2MSFTNGP06.phx.gbl...
> >> Using ASP with VBScript is there a clever way to easily check if a
value
> >> exists in an array without looping through it?
> >
> > 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



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.

--
Anthony Jones - MVP ASP/ASP.NET

Re: How do you check a value exists in an array?

am 18.01.2008 09:47:35 von Dooza

Bob Barrows [MVP] wrote:
> Dooza wrote:
>> Bob Barrows [MVP] wrote:
>>>>> 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.
>>>>
>>> ? 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?
>>
> 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