www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reuse/reset dynamic rectangular array?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
How can I reset a rectangualr array without having to loop through it?

int[][] myRectData = new int[][](10,10);

myRectData.length = 0;
myRectData[].length = 0;		
myRectData[][].length = 0;	

They all give: slice expression .. is not a modifiable lvalue.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
May 25 2019
next sibling parent matheus <matheus gmail.com> writes:
On Saturday, 25 May 2019 at 14:28:24 UTC, Robert M. Münch wrote:
 How can I reset a rectangualr array without having to loop 
 through it?

 int[][] myRectData = new int[][](10,10);

 myRectData.length = 0;
 myRectData[].length = 0;		
 myRectData[][].length = 0;	

 They all give: slice expression .. is not a modifiable lvalue.
This works form me: //DMD64 D Compiler 2.072.2 import std.stdio; void main() { auto arr = new int[][](10,10); writeln(arr.length); arr.length=0; writeln(arr.length); } output: 10 0 Matheus.
May 25 2019
prev sibling parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-05-25 14:28:24 +0000, Robert M. Münch said:

 How can I reset a rectangualr array without having to loop through it?
 
 int[][] myRectData = new int[][](10,10);
 
 myRectData.length = 0;
 myRectData[].length = 0;		
 myRectData[][].length = 0;	
 
 They all give: slice expression .. is not a modifiable lvalue.
My question was unprecise: I want to keep the first dimension and only reset the arrays of the 2nd dimension. So that I can append stuff again. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
May 25 2019
parent reply 9il <ilyayaroshenko gmail.com> writes:
On Saturday, 25 May 2019 at 16:17:40 UTC, Robert M. Münch wrote:
 On 2019-05-25 14:28:24 +0000, Robert M. Münch said:

 How can I reset a rectangualr array without having to loop 
 through it?
 
 int[][] myRectData = new int[][](10,10);
 
 myRectData.length = 0;
 myRectData[].length = 0;		
 myRectData[][].length = 0;	
 
 They all give: slice expression .. is not a modifiable lvalue.
My question was unprecise: I want to keep the first dimension and only reset the arrays of the 2nd dimension. So that I can append stuff again.
myRectData[] = null;
May 27 2019
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-05-28 01:52:28 +0000, 9il said:

 myRectData[] = null;
:-/ Ok... doesn't look to complicated ;-) Still learning a lot about D... -- Robert M. Münch http://www.saphirion.com smarter | better | faster
May 28 2019
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/28/19 4:58 PM, Robert M. Münch wrote:
 On 2019-05-28 01:52:28 +0000, 9il said:
 
 myRectData[] = null;
:-/ Ok... doesn't look to complicated ;-) Still learning a lot about D...
Keep in mind, this does not reuse the allocated arrays, it simply resets them to point at nothing (and will need to be reallocated). To reset the way you want, you need a loop: foreach(ref arr; myRectData) { arr.length = 0; arr.assumeSafeAppend; } -Steve
May 28 2019