Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • D
import std.stdio;
foreach(i, x; items)
{
   writefln("%s: %s", i, x);
}

use 'ref' on the item variable 'x' to access by reference
import std.stdio, std.algorithm;
import std.range;
items.enumerate.each!(a => writeln("i = ", a[0], " value = ", a[1]));

enumerate can be used instead of foreach with an index when the statement is simple enough to be a lambda or when the foreach aggregate (the array like thing) can't be indexed.
with Ada.Text_IO;
use Ada.Text_IO;
for I in Items'Range loop
   X := Items (I);
   Put_Line (Integer'Image (I) & " " & Integer'Image (X));
end loop;

Assuming Items is an array of integers.

New implementation...