How do you check a value exists in an array?

Discussion in 'ASP General' started by Dooza, Jan 17, 2008.

  1. Dooza

    Dooza Guest

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

    Steve
     
    Dooza, Jan 17, 2008
    #1
    1. Advertising

  2. 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.
     
    Bob Barrows [MVP], Jan 17, 2008
    #2
    1. Advertising

  3. Dooza

    Dooza Guest

    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
     
    Dooza, Jan 17, 2008
    #3
  4. Dooza

    Dooza Guest

    McKirahan wrote:
    > "Dooza" <> wrote in message
    > news:OILNg$...
    >> 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
    %>
     
    Dooza, Jan 17, 2008
    #4
  5. "McKirahan" <> wrote in message
    news:...
    > "Dooza" <> wrote in message
    > news:OILNg$...
    > > 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
     
    Anthony Jones, Jan 17, 2008
    #5
  6. "Dooza" <> wrote in message
    news:OILNg$...
    > 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
     
    Anthony Jones, Jan 17, 2008
    #6
  7. Dooza

    Dooza Guest

    Anthony Jones wrote:
    > "Dooza" <> wrote in message
    > news:OILNg$...
    >> 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
     
    Dooza, Jan 17, 2008
    #7
  8. Anthony Jones wrote:
    > "McKirahan" <> wrote in message
    > news:...
    >> "Dooza" <> wrote in message
    >> news:OILNg$...
    >>> 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.
     
    Bob Barrows [MVP], Jan 17, 2008
    #8
  9. Dooza wrote:
    > Anthony Jones wrote:
    >> "Dooza" <> wrote in message
    >> news:OILNg$...
    >>> 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.
     
    Bob Barrows [MVP], Jan 17, 2008
    #9
  10. Dooza

    Dooza Guest

    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
     
    Dooza, Jan 17, 2008
    #10
  11. 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.
     
    Bob Barrows [MVP], Jan 17, 2008
    #11
  12. "Bob Barrows [MVP]" <> wrote in message
    news:...
    > Anthony Jones wrote:
    > > "McKirahan" <> wrote in message
    > > news:...
    > >> "Dooza" <> wrote in message
    > >> news:OILNg$...
    > >>> 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
     
    Anthony Jones, Jan 17, 2008
    #12
  13. "Dooza" <> wrote in message
    news:...
    > McKirahan wrote:
    > > "Dooza" <> wrote in message
    > > news:OILNg$...
    > >> 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


    <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.

    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Jan 17, 2008
    #13
  14. Dooza

    Dooza Guest

    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
     
    Dooza, Jan 18, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Totan
    Replies:
    0
    Views:
    1,084
    Totan
    Apr 17, 2006
  2. =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=

    Dropdown List - Check if value exists in list prior to selection

    =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=, Mar 10, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    2,019
    =?Utf-8?B?QnJlbmRlbg==?=
    Mar 20, 2007
  3. Jason Stacy
    Replies:
    2
    Views:
    4,868
    Roedy Green
    Apr 27, 2008
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,310
  5. JDP@Work
    Replies:
    1
    Views:
    143
    JDP@Work
    Dec 19, 2005
Loading...

Share This Page