<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Проверка вхождения в массив]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11981&amp;type=atom" />
	<updated>2016-09-04T19:05:29Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=11981</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Проверка вхождения в массив]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=107315#p107315" />
			<content type="html"><![CDATA[<p>Может, на коллекции перейти?<br /></p><div class="codebox"><pre><code>List := {0:&quot;&quot;, 2:&quot;&quot;, 4:&quot;&quot;, 6:&quot;&quot;, 8:&quot;&quot;}
Loop % List.Length()
msgBox % (List.HasKey(A_Index) ? &quot;есть&quot; : &quot;нет&quot;) &quot; &quot;A_Index</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Flasher]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=27593</uri>
			</author>
			<updated>2016-09-04T19:05:29Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=107315#p107315</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: Проверка вхождения в массив]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=107309#p107309" />
			<content type="html"><![CDATA[<p>Часто вместо массивов используется объединение всех переменных в одну строку.<br />Проверить наличие переменной в таком &quot;массиве&quot; несложно: IfInString, переменная, массив.<br />Также можно написать (x ~= &quot;MyRegExp1|MyRegExp2|MyRegExp3&quot;).</p><p>Можно ли так же лаконично проверять это в настоящих массивах?<br />Пока не придумал ничего лучше, чем написать самописную функцию:<br /></p><div class="codebox"><pre><code>Array:=[1, 5, 7], x:=3, y:=5
MsgBox % indexOf(x, Array) &quot; | &quot; indexOf(y, [8, 5, 0, 5], 3)

; Возвращает индекс первого вхождения значения в массиве.
; Индекс массива, с которого начинается поиск. Если параметр fromIndex опущен, то поиск начинается с индекса 1.
indexOf(var, Arr, fromIndex:=1) {
	Loop % Arr.MaxIndex() - fromIndex + 1
		If (var = Arr[A_Index + fromIndex - 1])
			Return A_Index + fromIndex - 1
	Return false
}</code></pre></div><p>В js у массивов есть метод indexOf(var), в php есть ф-ия in_array(x, Array).<br />А это питон:<br /></p><div class="codebox"><pre><code>if &#039;a&#039; in [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;]:
    print &quot;YES&quot;
else:
    print &quot;NO&quot;</code></pre></div><p>Круто, да?<br />Я постоянно встречаю в чужих скриптах разные конструкции, склеивающие список значений в одну строку, затем парсящие эту строку (бр-р-р), и сам иногда пишу код вида (x=a || x=b || ... || x=e). Хотелось бы делать это красиво и просто, такие дела.</p>]]></content>
			<author>
				<name><![CDATA[stealzy]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=31937</uri>
			</author>
			<updated>2016-09-04T16:28:08Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=107309#p107309</id>
		</entry>
</feed>
