【问题标题】:Searching array with an If Statement ASP使用 If 语句 ASP 搜索数组
【发布时间】:2025-12-09 17:35:02
【问题描述】:

我有这个函数来搜索一个值是否通过数组存在。这是我的功能:

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 & ")

【问题讨论】:

  • 这是 Visual Basic 吗?
  • iCountryArray 到底是什么?是字符串吗?
  • @melpomene response.write 的出现和函数中缺少类型表明我们正处于经典 ASP 中。而 iCountryArray 是一个数字数组。
  • @VanquiishedWombat 我不知道 ASP 是什么。我只是在寻找更好的标签。 :-)
  • 哦,好的。那么 VBScript 和 asp-classic 是合适的。数组是边界,if 语句不合适。

标签: arrays vbscript asp-classic


【解决方案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

【讨论】:

    【解决方案2】:

    被另一个用例带走了一点,为了在这里提供更直接的答案,一种方法,在该方法中,您的国家代码字符串保持为字符串,我们只需查看子字符串是否在该字符串中。 为了确保整个子字符串都在那里,我使用分词符(你的字符串中的那些)来包围搜索和搜索的字符串。

    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 中,支票和打印可以是单行的

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

    OR 使用正则表达式

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

    一些解释:在""#{}包围的字符串中插入代码,这是一种强大的连接方式。 .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
    

    【讨论】:

    • 这怎么可能是远程动态的?
    • 你是对的,在另一个用例中被带走了,添加了一个更重要的答案