Jump to page: 1 2 3
Thread overview
Finding position of a value in an array
Dec 29, 2019
Daren Scot Wilson
Dec 29, 2019
mipri
Dec 29, 2019
MoonlightSentinel
Dec 29, 2019
Ron Tarrant
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
mipri
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
MoonlightSentinel
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
mipri
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
Ron Tarrant
Dec 30, 2019
mipri
Dec 30, 2019
JN
Dec 31, 2019
Daren Scot Wilson
Dec 31, 2019
Paul Backus
Dec 31, 2019
Daren Scot Wilson
Dec 31, 2019
bachmeier
Dec 31, 2019
MoonlightSentinel
Feb 06, 2021
mw
Feb 06, 2021
Ali Çehreli
Feb 07, 2021
mw
Dec 31, 2019
H. S. Teoh
Feb 06, 2021
Rumbu
Feb 06, 2021
Imperatorn
December 29, 2019
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.

December 29, 2019
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);

It might not be an obvious application of countUntil, but it's
listed at https://dlang.org/phobos/std_algorithm_searching.html
December 29, 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);
December 29, 2019
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel wrote:
> 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:

>> int i = a.countUntil!(v => v == 55);
>> assert(i == 2);
>
> A predicate isn’t required, countUntil accepts single elements:
>
> int i = a.countUntil(55);

I was just about to go looking for something like this. Thanks, guys.
December 30, 2019
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel wrote:

> int i = a.countUntil(55);


I was trying to do this with an array of pointers, but I get an error (which suggests to me that I don't know what data type a pointer is):

find_in_array_object.d(25): Error: cannot cast expression newObject of type find_in_array_object.MyObject to ulong
find_in_array_object.d(34): Error: template std.algorithm.searching.countUntil cannot deduce function from argument types !()(MyObject[], ulong), candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(768):        std.algorithm.searching.countUntil(alias pred = "a == b", R, Rs...)(R haystack, Rs needles) if (isForwardRange!R && (Rs.length > 0) && (isForwardRange!(Rs[0]) == isInputRange!(Rs[0])) && is(typeof(startsWith!pred(haystack, needles[0]))) && (Rs.length == 1 || is(typeof(countUntil!pred(haystack, needles[1..__dollar])))))
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(856):        std.algorithm.searching.countUntil(alias pred = "a == b", R, N)(R haystack, N needle) if (isInputRange!R && is(typeof(binaryFun!pred(haystack.front, needle)) : bool))
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(915):        std.algorithm.searching.countUntil(alias pred, R)(R haystack) if (isInputRange!R && is(typeof(unaryFun!pred(haystack.front)) : bool))

It's not a ulong? Have I forgotten so much?
December 30, 2019
On Monday, 30 December 2019 at 14:30:12 UTC, Ron Tarrant wrote:
> On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel wrote:
>
>> int i = a.countUntil(55);
>
>
> I was trying to do this with an array of pointers, but I get an error (which suggests to me that I don't know what data type a pointer is):
>
> find_in_array_object.d(25): Error: cannot cast expression newObject of type find_in_array_object.MyObject to ulong

What's your code? 'find_in_array_object.MyObject' doesn't look
like a pointer.
December 30, 2019
On Monday, 30 December 2019 at 14:30:12 UTC, Ron Tarrant wrote:
> I was trying to do this with an array of pointers, but I get an error (which suggests to me that I don't know what data type a pointer is):
>
> It's not a ulong? Have I forgotten so much?

D disallows implicit conversion from integers to pointers and hence they cannot be compared. You would need to explicitly cast your ulong to an appropriate pointer type
December 30, 2019
On Monday, 30 December 2019 at 14:41:55 UTC, mipri wrote:

> What's your code? 'find_in_array_object.MyObject' doesn't look
> like a pointer.

It's an array of objects... or, what it's trying to be, an array of object pointers.
December 30, 2019
On Monday, 30 December 2019 at 17:12:26 UTC, MoonlightSentinel wrote:

> D disallows implicit conversion from integers to pointers and hence they cannot be compared. You would need to explicitly cast your ulong to an appropriate pointer type

I'm not trying to convert, just wade through an array of pointers to find a specific pointer using searchUntil().

I mean, it's not a big deal if I can't do it. Adding an ID property and searching on that is just as effective and likely just as efficient.
December 30, 2019
On Monday, 30 December 2019 at 19:08:27 UTC, Ron Tarrant wrote:

> I'm not trying to convert, just wade through an array of pointers to find a specific pointer using searchUntil().

****** that should read: countUntil(), not searchUntil() *****

Turns out I was getting too complicated. countUntil() works on object references as well, even if printing object references to a shell doesn't seem to differentiate between them.
« First   ‹ Prev
1 2 3