2016-04-22 64 views
0

我試圖檢查一個變量的值是否在數組中。有一些困難。我的數組存儲在global.asa中。檢查經典的asp數組中的變量

Dim aTree(5) 
    aTree(0) = "a" 
    aTree(1) = "b" 
    aTree(2) = "c" 
    aTree(3) = "d" 
    aTree(4) = "e" 
    aTree(5) = "f" 
     Application("aTree") = aTree 

變量我試圖檢查商店的一些HTML內容

<% If tree = "a" Then %> 
    <div class="card bg_white"> 
      <h1 class="bold small-caps tbrown">my content</h1> 
    </div> 
<% End If %> 

我試圖做檢查這樣

<% tree = Request("tree") 
if in_array(tree, aTree) then %> 
You've found it 
<% End if %> 

我有了這個笨重的版本該作品

(tree <> "" and tree <> "a" and tree <> "b" and tree <> "c" and tree <> "d" and tree <> "e" and tree <> "f") 

Bu我想用一個更優雅的方式來處理數組。

幫助不大。謝謝。

回答

4

沒有內置InArray()函數,但它應該足夠簡單,可以構建自己的函數。

Function InArray(theArray,theValue) 
    dim i, fnd 
    fnd = False 
    For i = 0 to UBound(theArray) 
     If theArray(i) = theValue Then 
      fnd = True 
      Exit For 
     End If 
    Next 
    InArray = fnd 
End Function 

修改此函數以返回值的索引而不是隻是true/false作爲讀者的練習。 :)