delete an element from an array
adam.ciganek at gmail.com
Sun Oct 24 13:53:38 PDT 2010
> Your new example doesn't show it better, it's the only one you've given that
> shows it at all. What you had originally was
> auto a = [1, 2, 3, 4, 5, 6];
> auto b = delete(a, 4);
> assert([1, 2, 3, 4, 6] == b);
> which shows the removal of the element at index 4, not the element with
> value 4.
Yep, I made a typo. The last line was supposed to be
assert([1,2,3,5,6] == b). My bad.
> But there are further questions to be answered:
> - What do you want it to do if the value isn't in the array? Just return
> the original array, return a copy of the original array, or throw an
Good question. I would return the array unchanged (or maybe returning
range pointing to the same data as the original array). The
justification is that since I'm going to delete the element anyway, I
probably "don't care" about it, so it does not matter if it's in the
array in the first place. Throwing an exception does not seem useful
> - What do you want it to do if the value is in the array more than once?
> Remove the first, remove the last, remove all of them, or pick one at
Another good question. In the end, there should probably be more
functions: deleteFirst, deleteLast, deleteAll or even deleteRandom.
More information about the Digitalmars-d-learn