Finding position of a value in an array

MoonlightSentinel moonlightsentinel at disroot.org
Sun Dec 29 09:44:18 UTC 2019


On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote:
> On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson 
> wrote:
>> Reading documentation... Array, Algorithms, ... maybe I've 
>> been up too late... how does one obtain the index of, say, 55 
>> in an array like this
>>
>>     int[] a = [77,66,55,44];
>>
>> I want to do something like:
>>
>>     int i = a.find_value_returning_its_index(55);
>>     assert(i==2)
>>
>> I'm sure it's obvious but I'm not seeing it right now.
>
> int i = a.countUntil!(v => v == 55);
> assert(i == 2);

A predicate isn’t required, countUntil accepts single elements:

int i = a.countUntil(55);


More information about the Digitalmars-d-learn mailing list