how to assign multiple variables at once by unpacking array?
Andrea Fontana
nospam at example.org
Sun Oct 8 07:44:04 UTC 2023
On Saturday, 7 October 2023 at 07:31:45 UTC, mw wrote:
> https://stackoverflow.com/questions/47046850/is-there-any-way-to-assign-multiple-variable-at-once-with-dlang
>
> How to do this Python code in D:
>
> ```
>>>> s = "1 2 3"
>>>> A,B,C = map(int, s.split(" "))
>>>> A,B,C
> (1, 2, 3)
>
> ```
>
> Is there a better way (since 2017)?
Ranges for the win!
```
int a,b,c;
"1,2,3"
.splitter(',')
.zip(only(&a, &b, &c))
.each!(x => *x[1] = x[0].to!int);
writeln(a, b, c);
```
More information about the Digitalmars-d-learn
mailing list