Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用If语句搜索数组_Arrays_Vbscript_Asp Classic - Fatal编程技术网

Arrays 使用If语句搜索数组

Arrays 使用If语句搜索数组,arrays,vbscript,asp-classic,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 T

我有这个函数搜索数组中是否存在值。以下是我的功能:

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
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
这个很好用。我的问题是数组(99115218305)中的值是动态的。我有一个通过查询创建该值的变量

iCountryArray是我的变量,它存储值99115218305。因此,我需要动态地添加这些值,而不是手动将这些值输入到数组中

这不起作用:

CountryArray = Array(" & iCountryArray & ")

如果您的iCountryArray将国家/地区编号连接到其中,则可以使用
Split()
函数生成数组。这意味着您可以拥有所需的任意多个国家/地区号码。请注意,字符串将拆分为字符串数组,并且您需要一个数字测试,因此我们在函数中使用
cLng()
函数:

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

被另一个用例吸引住了,这里提供一个更直接的答案,一个方法,在这个方法中,您的countrycodes字符串保持为字符串,我们只需查看子字符串是否在该字符串中。 为了确保整个子字符串都在那里,我使用demimiter(字符串中的那些)来封装搜索和搜索字符串

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
注意,这不是答案的一部分,但因为我所有的脚本都是用Ruby编写的,所以这里是Ruby版本。 在Ruby中,支票和打印可以是一行

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

这是Visual Basic吗?什么是
iCountryArray
?它是一个字符串吗?@melpomene
response.write的出现以及函数中缺少类型说明了一个很大的提示,即我们使用的是经典的ASP。而iCountryArray是一个数字数组。@VanquishedWombat我不知道ASP是什么。我只是在寻找更好的标签。:-)哦,好的。那么VBScript和asp经典是合适的。数组是有边界的,if语句不合适。这是怎么一个远程动态的呢?你说得对,在另一个用例中被冲昏头脑,添加了一个更切题的答案