Disjoint slices of an array as reference

data pulverizer data.pulverizer at gmail.com
Thu Aug 20 02:21:15 UTC 2020


I have been trying to create a new array from an existing array 
that is effectively a view on the original array. This can be 
done with slices in D:

```
auto x = [1,  2,  3, 4,  5,
            6,  7,  8, 9,  10,
          11, 12, 13, 14, 15];
auto y = [0..5];
```

y a subset of the x array. If I do:

```
y[0] = 11;
```

x[0] will be modified to 11;

However I would like to have disjoint slices, something like this:

```
auto y = x[0..5, 9..14];
```

Which selects different disjoint parts of the array x, but is not 
allowed on T[]. I have tried something like:

```
double[] y;
y ~= x[0..5];
y ~= x[9..14];
```

But the act of appending results in array copying - breaks the 
reference with the original array. The only other thing I have 
considered is creating an array of references to each of the 
elements of x I would like but that just seems like overkill.

It would be good to know if there is a more straightforward way 
of doing this type of disjoint selection.

Thanks in advance.






More information about the Digitalmars-d-learn mailing list