Dynamic array handling
Ali Çehreli
acehreli at yahoo.com
Thu Nov 14 13:50:53 PST 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
More information about the Digitalmars-d-learn
mailing list