Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add to a dynamic array in D?

Tags:

d

I must be missing something obvious now, but I can't figure out how to add elements to a dynamic array in D.

I have tried this, without success:

string[] links;
foreach(link; someOtherArray) {
    // Do something with link ...
    links[] = link; // Trying here to add to the links array
}

and this:

string[] links;
int i = 0;
foreach(link; someOtherArray) {
    // Do something with link ...
    links[i] = link; // Trying here to add to the links array
    i++;
}

What is the correct way to do this?

like image 623
Samuel Lampa Avatar asked Apr 25 '26 21:04

Samuel Lampa


1 Answers

Use the concat operator: a ~ b or a ~= b;

string[] links;
foreach(link; arr) {
     links ~= link;
}

The right side can be an individual element or another array.

like image 54
Adam D. Ruppe Avatar answered May 04 '26 06:05

Adam D. Ruppe