'in' syntax for searching arrays

Regan Heath regan at netmail.co.nz
Mon Nov 5 03:11:31 PST 2007


Janice Caron wrote:
> How coincidental. Only yesterday (on another thread) I posted my
> thought that it would be cool to have a std.array module with lots of
> array functions, with as much power as those of PHP (though not with
> the same syntax!)
> 
> My own thought is that looking for values in arrays should have the
> same syntax as strings. That is, for non-associative arrays:
> 
> V[] a;
> int n = a.find(x);
> 
> should return -1 if the value is not present in the array, or the
> index if it is. This is a much better idea than returning a pointer to
> the element, because (a) you don't have to worry about const and
> invariant, and (b) it's the same as std.string. In addition, you can
> then do a[n..$].find() to find the second occurence, and so on.

You do have to remember to add the result of the 2nd call to the result 
of the first to get the index of the 2nd occurrence though.  I've 
forgotten to do it almost as many times as I've coded something using this.

Regan



More information about the Digitalmars-d mailing list