2017-01-25 8 views
2

配列を介して値が存在するかどうかを調べる関数です。ここに私の機能があります:Ifステートメントを使用して配列を検索するASP

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を格納する私の変数です。だから私の配列にそれらの値を手動で入力する代わりに、私はそれらを動的に追加する必要があります。

これは動作しません:あなたのiCountryArrayがそれに連結国番号を持つ

CountryArray = Array(" & iCountryArray & ") 
+1

これはVisual Basicですか? – melpomene

+0

はい。これをClassic ASP Webアプリケーションに入れようとしています – Brasciole

+1

「iCountryArray」とは何ですか?それは文字列ですか? – melpomene

答えて

1

ならば、あなたは、配列を作るために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 
0

ガットは、より直接的に提供するために、代わりのユースケースによって少し離れ実施ここでは、あなたの国コードの文字列が文字列のままであり、部分文字列がその文字列内にあるかどうかを調べるメソッドに答えます。 部分文字列全体を確実にするために、私はデミミター(あなたの文字列の中にあるもの)を使用して、検索と検索文字列を囲みます。

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" 

または正規表現

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

一部explenationを使用してonelinerが考えられます。""#{}で囲まれた文字列内の内のコードを挿入し、それが強力な方法であります連結する。 .match(/\b#{this_country}\b/)は、それが可能であるか、ご利用の場合この場合には、変数this_country

が分からないのですが、次のコードを見て、それは便利..

であると思い、 //で囲まれた文字列にマッチする正規expresionを使用しています

カントリーコードと名前の辞書を使用して、このようにそれを使用することができます。..

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

これはリモートでもどのように動的ですか? – Martha

+0

You'r right、もう1つのユースケースで逃げ出した – peter

関連する問題