Thread overview
What am I doing wrong ?
Apr 22, 2012
SomeDude
Apr 22, 2012
Dmitry Olshansky
Apr 22, 2012
SomeDude
Apr 22, 2012
Mike Wey
Apr 26, 2012
Marco Leise
April 22, 2012
Sorry for the noob questions, but

import std.stdio;

struct Foo {
    int x;
}
void main() {
    auto array = new Foo[10];
    auto i = array.length;
    foreach(Foo f; array) { f.x = --i; write(f.x);}
    writeln();
    foreach(Foo f; array) { write(f.x);}
}

gives me:

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
9876543210
0000000000

Also,

void main() {
    auto array = new Foo[10];
--> for(int i = array.length; i > 1; i--) { array[i].x = i; }
    writeln();
    foreach(Foo f; array) { write(f.x);}
}

throws core.exception.RangeError@bug(8): Range violation on the line with the arrow.

What am I doing wrong ?
April 22, 2012
On 23.04.2012 1:47, SomeDude wrote:
> Sorry for the noob questions, but
>
> import std.stdio;
>
> struct Foo {
> int x;
> }
> void main() {
> auto array = new Foo[10];
> auto i = array.length;
> foreach(Foo f; array) { f.x = --i; write(f.x);}
> writeln();
> foreach(Foo f; array) { write(f.x);}

Here: Foo f is not a reference but a copy of each array element. Use
foreach( ref f; array)

> }
>
> gives me:
>
> PS E:\DigitalMars\dmd2\samples> rdmd bug.d
> 9876543210
> 0000000000
>
> Also,
>
> void main() {
> auto array = new Foo[10];
> --> for(int i = array.length; i > 1; i--) { array[i].x = i; }

Arrays indices are 0 based, thus last index is array.length-1.

> writeln();
> foreach(Foo f; array) { write(f.x);}
> }
>
> throws core.exception.RangeError@bug(8): Range violation on the line
> with the arrow.
>
> What am I doing wrong ?


-- 
Dmitry Olshansky
April 22, 2012
On 04/22/2012 11:47 PM, SomeDude wrote:
> Sorry for the noob questions, but
>
> import std.stdio;
>
> struct Foo {
> int x;
> }
> void main() {
> auto array = new Foo[10];
> auto i = array.length;
> foreach(Foo f; array) { f.x = --i; write(f.x);}

Use ref when you want to modify the original array, without ref you are modifying a copy in the foreach.

foreach(ref Foo f; array) { f.x = --i; write(f.x);}

> writeln();
> foreach(Foo f; array) { write(f.x);}
> }
>
> gives me:
>
> PS E:\DigitalMars\dmd2\samples> rdmd bug.d
> 9876543210
> 0000000000
>
> Also,
>
> void main() {
> auto array = new Foo[10];
> --> for(int i = array.length; i > 1; i--) { array[i].x = i; }
> writeln();
> foreach(Foo f; array) { write(f.x);}
> }
>
> throws core.exception.RangeError@bug(8): Range violation on the line
> with the arrow.
>
> What am I doing wrong ?

You set i to the array length of 10 but the array index is zero based so the last item in the array is at index 9.
But in the first iteration of the loop you are trying to access the the item at index 10 (array.length) which doesn't exist.

-- 
Mike Wey
April 22, 2012
On Sunday, 22 April 2012 at 21:50:32 UTC, Dmitry Olshansky wrote:

Omagad, thank you, too much Java is baaaad for your brains.
April 26, 2012
Am Sun, 22 Apr 2012 23:47:20 +0200
schrieb "SomeDude" <lovelydear@mailmetrash.com>:

> void main() {
>      auto array = new Foo[10];
> --> for(int i = array.length; i > 1; i--) { array[i].x = i; }
>      writeln();
>      foreach(Foo f; array) { write(f.x);}
> }
> 
> throws core.exception.RangeError@bug(8): Range violation on the line with the arrow.
> 
> What am I doing wrong ?

You could also try:

foreach_reverse(i, ref f; array) { f.x = i; }

-- 
Marco