Dynamic array and foreach loop

Binarydepth via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 9 12:09:59 PDT 2015


On Sunday, 9 August 2015 at 16:42:16 UTC, Jay Norwood wrote:
> The i+3 initialization is just so you can see that v is the Arr 
> member (not the index) in the other loops.
>
> import std.stdio : writeln;
> void main()     {
> 	immutable a=5;
> 	int[a] Arr;
> 	foreach(i, ref v; Arr)      {
> 		v = i+3;
> 	}
> 	foreach( ref v; Arr)      {
> 		writeln(v);
> 	}
> 	foreach( c; Arr)      {
> 		writeln(c);
> 	}
> }

Oooh... I like how this works

import std.stdio : writeln, readf;
void main()     {
         immutable a=5;
         int[a] Arr;
         int nim;
         foreach(num, ref nem; Arr)      {
                 readf(" %s", &nem);
         }
         foreach(num; Arr)      {
                 writeln(num);
         }
}


More information about the Digitalmars-d-learn mailing list