digitalmars.D.learn - Bidimensional dynamic array problem
- orgoton (22/22) Mar 06 2007 I have this
- orgoton (7/11) Mar 06 2007 should be
- Frits van Bommel (19/46) Mar 06 2007 foreach(inout Vertex[] strip; vData) strip.length = sizeY;
- Frits van Bommel (3/21) Mar 06 2007 Sorry, that's not right. Not even the data is modified, just the data
- orgoton (2/59) Mar 06 2007 That did it! Thanks a lot. I feel illuminated :P
I have this struct Vertex{ (...) float z; } then: Vertex vData[][]; Somewhere later: vData.length=sizeX; foreach(Vertex strip[]; vData[]) strip.length=sizeY; Here the array is initialized. So far, I think I haven't done anything illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how many strips I want, and next, I put in how many elements that strip will have. ulong m; foreach (Vertex[] strip; vData) { foreach (Vertex vertex; strip) { file.read(vertex.height); m++; } } Here I read the values from a file. First, I select a strip, and then go through all the elements of that array and read their values. "m" is a counter to see how many elements where read. The thing is, m=0 at the end of the loop. What did I do wrong?
Mar 06 2007
Just a minor correction:struct Vertex{ (...) float z; }should be struct Vertex{ (...) float height; } (typo)
Mar 06 2007
orgoton wrote:I have this struct Vertex{ (...) float z; } then: Vertex vData[][]; Somewhere later: vData.length=sizeX; foreach(Vertex strip[]; vData[]) strip.length=sizeY;foreach(inout Vertex[] strip; vData) strip.length = sizeY; or just: foreach(inout strip; vData) strip.length = sizeY; Note: only the inout is significant, the rest is just nitpicking :P. If you don't specify 'inout', you're just modifying a local copy of the element... And IIRC something like "vData = new Vertex[][](sizeX, sizeY)" should replace that entire code sequence, initializing the outer array to an array of sizeX arrays of length sizeY.Here the array is initialized. So far, I think I haven't done anything illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how many strips I want, and next, I put in how many elements that strip will have.You haven't done anything illegal, just something wrong ;). You haven't initialized the array the way you thought you did: it's just an array of _empty_ dynamic arrays at this point.ulong m; foreach (Vertex[] strip; vData)(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references){ foreach (Vertex vertex; strip)foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip)){ file.read(vertex.height); m++; } } Here I read the values from a file. First, I select a strip, and then go through all the elements of that array and read their values. "m" is a counter to see how many elements where read. The thing is, m=0 at the end of the loop. What did I do wrong?Since you didn't initialize the elements of vData, their lengths are all still zero. So your inner loop is never executed.
Mar 06 2007
Frits van Bommel wrote:orgoton wrote:Sorry, that's not right. Not even the data is modified, just the data referenced *by* the data...ulong m; foreach (Vertex[] strip; vData)(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references){ foreach (Vertex vertex; strip)foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip)){ file.read(vertex.height); m++; } }
Mar 06 2007
Frits van Bommel Wrote:orgoton wrote:That did it! Thanks a lot. I feel illuminated :PI have this struct Vertex{ (...) float z; } then: Vertex vData[][]; Somewhere later: vData.length=sizeX; foreach(Vertex strip[]; vData[]) strip.length=sizeY;foreach(inout Vertex[] strip; vData) strip.length = sizeY; or just: foreach(inout strip; vData) strip.length = sizeY; Note: only the inout is significant, the rest is just nitpicking :P. If you don't specify 'inout', you're just modifying a local copy of the element... And IIRC something like "vData = new Vertex[][](sizeX, sizeY)" should replace that entire code sequence, initializing the outer array to an array of sizeX arrays of length sizeY.Here the array is initialized. So far, I think I haven't done anything illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how many strips I want, and next, I put in how many elements that strip will have.You haven't done anything illegal, just something wrong ;). You haven't initialized the array the way you thought you did: it's just an array of _empty_ dynamic arrays at this point.ulong m; foreach (Vertex[] strip; vData)(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references){ foreach (Vertex vertex; strip)foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip)){ file.read(vertex.height); m++; } } Here I read the values from a file. First, I select a strip, and then go through all the elements of that array and read their values. "m" is a counter to see how many elements where read. The thing is, m=0 at the end of the loop. What did I do wrong?Since you didn't initialize the elements of vData, their lengths are all still zero. So your inner loop is never executed.
Mar 06 2007