'How do you slice arrays in D?

How are arrays manipulated in D?



Solution 1:[1]

Here you can find a complete reference of array manipulations in D.

Solution 2:[2]

FYI. You can also copy with:

int[7] a;
int[] b;
b = a[5..7].dup;

Solution 3:[3]

To slice arrays, it's a simple matter of using

int[7] a;
int[] b;
b = a[5..7];

which sets b[0] to a[5] and b[1] to a[6]. But remember that this is a reference to the elements in a, not another copy of them. If you change b[0], this also affects a[5].

If you want to copy, you have to do:

int[7] a;
int[2] b;
b[0..1] = a[5..7];

This is because b is a static array; in the first code block, it was dynamic (effectively a pointer to somewhere within another array).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christian C. Salvadó
Solution 2
Solution 3