Thread overview
Dynamic array handling
Nov 14, 2013
seany
Nov 14, 2013
Brad Anderson
Nov 14, 2013
bearophile
Nov 14, 2013
Adam D. Ruppe
Nov 14, 2013
Ali Çehreli
Nov 14, 2013
seany
November 14, 2013
In Very High level languages, such as scilab, you can write

array_var = (1,2,3 ... etc)

and then you can also write

array_var = array_var(1:2,4:$)

In this case, the third element is dropped, and the same variable, array_var is set to be an array of a different length, resizing of array and so on is automated.

Is the same possible to be done in D?

say,

int [] a ; //initialize;

a ~= 1;
a ~= 2; //etc, polulate

. . . say, we fill up 10 such elements

Now, can you do like,

a = somefunction_that_drops_the_4th_element(a); // a is reset,
                                                // and the length
                                                // is reorganized automatically
November 14, 2013
On Thursday, 14 November 2013 at 21:38:39 UTC, seany wrote:
> In Very High level languages, such as scilab, you can write
>
> array_var = (1,2,3 ... etc)
>
> and then you can also write
>
> array_var = array_var(1:2,4:$)
>
> In this case, the third element is dropped, and the same variable, array_var is set to be an array of a different length, resizing of array and so on is automated.
>
> Is the same possible to be done in D?
>
> say,
>
> int [] a ; //initialize;
>
> a ~= 1;
> a ~= 2; //etc, polulate
>
> . . . say, we fill up 10 such elements
>
> Now, can you do like,
>
> a = somefunction_that_drops_the_4th_element(a); // a is reset,
>                                                 // and the length
>                                                 // is reorganized automatically

a = a.remove(3);
November 14, 2013
On Thursday, 14 November 2013 at 21:38:39 UTC, seany wrote:
> array_var = (1,2,3 ... etc)

In D, that'd look like:

auto array_var = [1,2,3,4,5....];

> array_var = array_var(1:2,4:$)

array_var = array_var[0 .. 1] ~ array_var[2 .. $];

array[x .. y] does a slice in D, with the first element of the array being element 0. The returned value is the array from x to y, not including y.

Then the ~ operator builds a new array out of the two pieces on either side. Note though: using ~ in a loop where you need high performance is generally a bad idea - it is convenient, but a little slow since it allocates space for a new array.

> a = somefunction_that_drops_the_4th_element(a); // a is reset,
>                                                 // and the

There's also a remove function in std.algorithm that can drop an item like this:

import std.algorithm;

array_var = array_var.remove(2); // removes the 3rd element (starting from 0, so index 2 is the third element)


This function btw performs better than the a[0 .. 1] ~ a[2 .. $] example above, since it modifies the array in place instead of building a new one.
November 14, 2013
On 11/14/2013 01:38 PM, seany wrote:
> In Very High level languages, such as scilab, you can write
>
> array_var = (1,2,3 ... etc)
>
> and then you can also write
>
> array_var = array_var(1:2,4:$)
>
> In this case, the third element is dropped, and the same variable,
> array_var is set to be an array of a different length, resizing of array
> and so on is automated.
>
> Is the same possible to be done in D?
>
> say,
>
> int [] a ; //initialize;
>
> a ~= 1;
> a ~= 2; //etc, polulate
>
> . . . say, we fill up 10 such elements
>
> Now, can you do like,
>
> a = somefunction_that_drops_the_4th_element(a); // a is reset,
>                                                  // and the length
>                                                  // is reorganized
> automatically

There is also chain() which works with ranges other than slices as well:

import std.array;
import std.range;
import std.algorithm;

void main()
{
    auto a = 10.iota.array;

    auto skipped = chain(a[0..3], a[4..$]);
    assert (skipped.equal([ 0, 1, 2, 4, 5, 6, 7, 8, 9 ]));
}

Ali

November 14, 2013
WOW

On Thursday, 14 November 2013 at 21:50:53 UTC, Ali Çehreli wrote:
> On 11/14/2013 01:38 PM, seany wrote:
>> In Very High level languages, such as scilab, you can write
>>
>> array_var = (1,2,3 ... etc)
>>
>> and then you can also write
>>
>> array_var = array_var(1:2,4:$)
>>
>> In this case, the third element is dropped, and the same variable,
>> array_var is set to be an array of a different length, resizing of array
>> and so on is automated.
>>
>> Is the same possible to be done in D?
>>
>> say,
>>
>> int [] a ; //initialize;
>>
>> a ~= 1;
>> a ~= 2; //etc, polulate
>>
>> . . . say, we fill up 10 such elements
>>
>> Now, can you do like,
>>
>> a = somefunction_that_drops_the_4th_element(a); // a is reset,
>>                                                 // and the length
>>                                                 // is reorganized
>> automatically
>
> There is also chain() which works with ranges other than slices as well:
>
> import std.array;
> import std.range;
> import std.algorithm;
>
> void main()
> {
>     auto a = 10.iota.array;
>
>     auto skipped = chain(a[0..3], a[4..$]);
>     assert (skipped.equal([ 0, 1, 2, 4, 5, 6, 7, 8, 9 ]));
> }
>
> Ali

November 14, 2013
Brad Anderson:

> a = a.remove(3);

But I think the remove function should be modified:
https://d.puremagic.com/issues/show_bug.cgi?id=10959

To the Original Poster I can also answer that with a filter+array functions you can build a new array that contains only certain items of the original array (according to a filtering function, or to a list of indexes to keep).

Bye,
bearophile