digitalmars.D.learn - Copying and array into another
- pascal111 (10/10) Aug 09 2022 I tried to copy an array into another without affecting in the
- Paul Backus (9/20) Aug 09 2022 A simpler way to do this is to use the array's built-in .dup
- pascal111 (3/26) Aug 09 2022 I don't know how I forgot this or why it didn't come to my mind
- pascal111 (1/1) Aug 09 2022 Edit "Copying an array into another" in the title.
I tried to copy an array into another without affecting in the
original array when I try to change the value of any element of
the new array, but I failed except with this way in the next code:
'''D
int[] x=[1,2,3];
int[] y=x.filter!("a==a").array;
y[1]=800;
x.writeln;
y.writeln;
'''
Aug 09 2022
On Tuesday, 9 August 2022 at 18:33:04 UTC, pascal111 wrote:
I tried to copy an array into another without affecting in the
original array when I try to change the value of any element of
the new array, but I failed except with this way in the next
code:
'''D
int[] x=[1,2,3];
int[] y=x.filter!("a==a").array;
y[1]=800;
x.writeln;
y.writeln;
'''
A simpler way to do this is to use the array's built-in .dup
method [1]:
int[] x = [1, 2, 3];
int[] y = x.dup;
y[1] = 800;
writeln(x); // [1, 2, 3]
writeln(y); // [1, 800, 3]
[1] https://dlang.org/spec/arrays.html#array-properties
Aug 09 2022
On Tuesday, 9 August 2022 at 18:41:52 UTC, Paul Backus wrote:On Tuesday, 9 August 2022 at 18:33:04 UTC, pascal111 wrote:I don't know how I forgot this or why it didn't come to my mind because I already indeed had read about ".dup"?! You are right!I tried to copy an array into another without affecting in the original array when I try to change the value of any element of the new array, but I failed except with this way in the next code: '''D int[] x=[1,2,3]; int[] y=x.filter!("a==a").array; y[1]=800; x.writeln; y.writeln; '''A simpler way to do this is to use the array's built-in .dup method [1]: int[] x = [1, 2, 3]; int[] y = x.dup; y[1] = 800; writeln(x); // [1, 2, 3] writeln(y); // [1, 800, 3] [1] https://dlang.org/spec/arrays.html#array-properties
Aug 09 2022
Edit "Copying an array into another" in the title.
Aug 09 2022









pascal111 <judas.the.messiah.111 gmail.com> 