On Monday, 4 April 2022 at 23:15:30 UTC, Enjoys Math wrote:

```d
// remove an item from an array
template drop(T)
{
  T drop( inout T[] arr, T which )
  {                     
    int i;
                T result;
                
                for (i=0; i < arr.length; i++)
                {
                        if (arr[i] == which)
                        {
                                result = arr[i];
                                break;
                        }
                }
                
                debug if ( which >= arr.length)
throw new Exception(str.format("Attempt to drop the %s of value %s from an array, but it was not found.", typeid(T), which));
                }
                
                for (; i < arr.length; i++)
                {
                        arr[i] = arr[i + 1];
                }
                arr.length = arr.length - 1;

                return result;
  }
}
```

I'm guessing you're doing this to learn and measure. You might be better off using the slicing method though. It's also possible to do it with a single loop:
```d
auto dropSlice(T)(T[] array, T which)
{
  T[] result;

  size_t i;                      // old index
  foreach(index, element; array)
  {
    if(element == which) {
      result ~= array[i..index]; // slice off
      i = index + 1;
    }
  }

  return result ~ array[i..$];   // last slice
}

void main()
{
  char[] dizi = "abcdefdg".dup;

  dizi.dropSlice('d').writeln;
```
SDB@79

Reply via email to