使用If语句ASP搜索数组

时间:2017-01-25 18:51:03

标签: arrays vbscript asp-classic

我有这个函数来搜索数组中是否存在值。这是我的职责:

Function in_array(iCountryID, iCountryArray)
in_array = False
For i=0 To Ubound(iCountryArray)
    If iCountryArray(i) = iCountryID Then
        in_array = True
        Exit Function      
     End If
 Next
 End Function

 ThisCountry = iCountryID
 CountryArray = Array(99,115,218,305)
 If in_array(ThisCountry, CountryArray) Then 
     Response.Write ThisCountry & " is in the array"
 Else 
     Response.Write ThisCountry & " is not in the array"
 End If

这很好用。我的问题是我的数组(99,115,218,305)中的值是动态的。我有一个通过查询创建该值的变量。

iCountryArray是我的变量,它存储值99,115,218,305。因此,我不需要手动将这些值输入到我的数组中,而是需要以动态方式添加它们。

这不起作用:

CountryArray = Array(" & iCountryArray & ")

2 个答案:

答案 0 :(得分:1)

如果您的iCountryArray已将国家/地区编号连接到其中,则可以使用Split()函数创建数组。这意味着您可以根据需要拥有尽可能多的国家/地区编号。请注意,字符串将拆分为字符串数组,并且您需要进行数字测试,因此我们在函数中包含cLng()函数:

Function in_array(iCountryID, iCountryArray)
  in_array = False
  For i=0 To Ubound(iCountryArray)
     If cLng(iCountryArray(i)) = iCountryID Then ' use cLng to convert to number type
        in_array = True
        Exit Function      
     End If
  Next
End Function

ThisCountry = iCountryID
CountryArray = split(sCountryList, ",") ' assuming sCountryList is like "12,33,55,3,99" etc
If in_array(ThisCountry, CountryArray) Then 
     Response.Write ThisCountry & " is in the array"
Else 
     Response.Write ThisCountry & " is not in the array"
End If

答案 1 :(得分:0)

通过一个替代用例稍微带走了一点,在这里提供一个更直接的答案,一个乡村代码字符串保持字符串的方法,我们只是查看子字符串是否在该字符串中。 为了确保整个子字符串存在,我使用demimiters(字符串中的那些)来包围搜索和搜索字符串。

CountryCodes = "99,115,218,305"
ThisCountry = "99"

Function Envelop(string)
  Envelop = "," & string & ","
End Function

Function InString(substring, string)
  Instring = (Instr(Envelop(string), Envelop(substring)) > 0)
End Function


If InString(ThisCountry, CountryCodes) Then 
  Wscript.Echo ThisCountry & " is in the string"
Else 
  Wscript.Echo ThisCountry & " is not in the string"
End If

NB不是答案的一部分,但由于我在Ruby中也使用Ruby版本完成了所有脚本。 在Ruby中,检查和打印可能是一个oneliner

country_codes = "99,115,218,305"
this_country  = "99"

puts "#{this_country} is " + (",#{country_codes},"[",#{this_country},"] ? "" : "not") + " in the string"

使用正则表达式

puts "#{this_country} is #{(country_codes.match(/\b#{this_country}\b/) ? "" : "not")} in the string"

一些explenation:在用"" #{}括起来的字符串中插入代码,它是一种强大的连接方式。 .match(/\b#{this_country}\b/)使用常规表达式匹配//中包含的字符串,在本例中为变量this_country

不知道是否可能或您的用例,但请看下面的代码,希望它有用..

您可以使用国家/地区代码和名称字典,并像这样使用它。

Set Countries = CreateObject("scripting.dictionary")
With Countries
  .Add "99", "Some Country"
  .Add "115", "Some other Country"
  .Add "218", "Still Some Country"
  .Add "305", "Another Country"
End With

Sub ShowCountry(code)
  code = Cstr(code)
  For Each country in Countries
    If Countries.Item(code) > "" Then
      Wscript.Echo Countries.Item(code)
      Exit Sub
    End If
  Next
  Wscript.Echo "Country with code " & code & " doesn't exist"
End Sub

ShowCountry 115 'Some other Country'
ShowCountry 9 'Country with code 9 doesn't exist'

此处还有Ruby版本

Countries = {99 => "Some Country", 115 => "Some other Country", 218 => "Still Some Country", 305 => "Another Country"}

def show_country code
  puts Countries[code] || "Country with code #{code} doesn't exist"
end

show_country 115 # Some other Country
show_country 9 #Country with code 9 doesn't exist