A converting problem in using "among" with arrays

H. S. Teoh hsteoh at qfbox.info
Fri Jul 29 22:43:11 UTC 2022


On Fri, Jul 29, 2022 at 10:32:11PM +0000, pascal111 via Digitalmars-d-learn wrote:
[...]
> I want searching for value 54 in array y "54.among(y).writeln;", but
> it seems compiler complaints because the data type is "int[]", so I
> tried to convert "y" to "uint[]".

You're using the wrong function; .among is intended to be used in the
cases where the list of values to search for are fixed or known at
compile-time.

If the values to search for changes at runtime, you want .canFind
instead. For example:

	if ([1, 2, 3, 4].canFind(3))
		writeln("found it!");

Or use .find if you need to retrieve the found value (e.g., if it's a
structure and you matched on only one field).

	auto r = [ 1, 2, 3, 4 ].find(3);
	assert(r.front == 3);


T

-- 
It's amazing how careful choice of punctuation can leave you hanging:


More information about the Digitalmars-d-learn mailing list