Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Foreach with an inout

Part of ForeachCategory

Description

An "inout" can be used to update the original elements.

Example

import std.stdio;

void main () {
  static uint[2] a = [7, 8];

  writefln("Original:");
  foreach (i, inout x; a) {
    writefln("  a[%d] == %d", i, x);
    x++;
  }

  writefln("Modified:");
  foreach (i, x; a)
    writefln("  a[%d] == %d", i, x);
}

Output

Original:
  a[0] == 7
  a[1] == 8
Modified:
  a[0] == 8
  a[1] == 9