www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Questions on array operations

reply stonecobra <scott stonecobra.com> writes:
Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is it:
array = array[0..4] ~ array[5..2];
Would this also work for a 2-dimensional array?

Q2.  If I have a 2 dimensional dynamic array, how would I clear it? 
Would it look something like:
array[][]=null;

Q3.  With the same 2-dimensional dynamic array, how do I set the size of 
the sencond dimension?  Is it:
array[].length = 5;

Q4. Again with the same array, how do I get the length of each 
dimension?  Is it:
int x = array.length;
int y = array[].length;

Thanks,
Scott Sanders
Jun 14 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
stonecobra wrote:

 Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is it:
 array = array[0..4] ~ array[5..2];

No. I presume that's a typo for array = array[0..4] ~ array[5..7]; but it's one way, and perhaps the simplest, asssuming you mean 5th in the sense of array[0] being the first.
 Would this also work for a 2-dimensional array?

Yes, but only in the outermost dimension.
 Q2.  If I have a 2 dimensional dynamic array, how would I clear it? 
 Would it look something like:
 array[][]=null;

What do you mean by clear? - set all values to zero? I'm not sure if array[][] = 0; works. If not, then this should: foreach (T[] row; array) row[] = 0; where T is whatever type the individual elements are. - make it a null array, i.e. one with no elements? array = null;
 Q3.  With the same 2-dimensional dynamic array, how do I set the size of 
 the sencond dimension?  Is it:
 array[].length = 5;

Multidimensional dynamic arrays are not rectangular. They are simply arrays of arrays. Each array within the outer array has an independent length. But you can do something like foreach (inout T[] row; array) row.length = 5;
 Q4. Again with the same array, how do I get the length of each 
 dimension?  Is it:
 int x = array.length;
 int y = array[].length;

Multidimensional dynamic arrays are not rectangular. They are simply arrays of arrays. Each array within the outer array has an independent length. But if you have set up all rows to have the same length, then you need only interrogate one of them: int y = array[0].length; If you really want dynamic rectangular arrays, you can either define your own class to do it, pick up one that someone's written (there probably are some around), or wait in the hope that Norbert's proposal http://homepages.uni-regensburg.de/~nen10015/documents/D-multidimarray.html be implemented. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jun 14 2004
parent reply Brad Anderson <brad dsource.dot.org> writes:
Stewart Gordon wrote:
 stonecobra wrote:
 
 Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is 
 it:
 array = array[0..4] ~ array[5..2];

No. I presume that's a typo for array = array[0..4] ~ array[5..7];

or array = array[0..4] ~ array[5..6] if it's an array of 7.
Jun 14 2004
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Brad Anderson" <brad dsource.dot.org> escribió en el mensaje
news:caknrk$1kee$1 digitaldaemon.com
| Stewart Gordon wrote:
|| stonecobra wrote:
||
||| Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is
||| it:
||| array = array[0..4] ~ array[5..2];
||
||
|| No.
||
|| I presume that's a typo for
||
||     array = array[0..4] ~ array[5..7];
|
| or
|
| array = array[0..4] ~ array[5..6]
|
| if it's an array of 7.

Wrong there. Slices are exclusive to the end (can someone tell me if that's
the right wording?)
array[5..7]=array[5]~array[6];

-----------------------
Carlos Santander Bernal
Jun 14 2004
next sibling parent Brad Anderson <brad sankaty.dot.com> writes:
Carlos Santander B. wrote:
 "Brad Anderson" <brad dsource.dot.org> escribió en el mensaje
 news:caknrk$1kee$1 digitaldaemon.com
 | Stewart Gordon wrote:
 || stonecobra wrote:
 ||
 ||| Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is
 ||| it:
 ||| array = array[0..4] ~ array[5..2];
 ||
 ||
 || No.
 ||
 || I presume that's a typo for
 ||
 ||     array = array[0..4] ~ array[5..7];
 |
 | or
 |
 | array = array[0..4] ~ array[5..6]
 |
 | if it's an array of 7.
 
 Wrong there. Slices are exclusive to the end (can someone tell me if that's
 the right wording?)
 array[5..7]=array[5]~array[6];
 
 -----------------------
 Carlos Santander Bernal
 
 

Yep, I think that's worded correctly. If I would have followed my own logic like the 5..6, I would have had 0..3, but you're correct that it's 0..4 and 5..7 Damn am I lazy. Can't even do a small example before I posted. BA
Jun 15 2004
prev sibling parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:calhks$2v9e$3 digitaldaemon.com...
 "Brad Anderson" <brad dsource.dot.org> escribió en el mensaje
 news:caknrk$1kee$1 digitaldaemon.com
 | Stewart Gordon wrote:
 || stonecobra wrote:
 ||
 ||| Q1.  What is the 'D' way to remove the 5th item from an array of 7? Is
 ||| it:
 ||| array = array[0..4] ~ array[5..2];
 ||
 ||
 || No.
 ||
 || I presume that's a typo for
 ||
 ||     array = array[0..4] ~ array[5..7];
 |
 | or
 |
 | array = array[0..4] ~ array[5..6]
 |
 | if it's an array of 7.

 Wrong there. Slices are exclusive to the end (can someone tell me if that's
 the right wording?)

In STL it's called an asymmetric range, with the notation [from,to). The ) represents that it's one past the end.
 array[5..7]=array[5]~array[6];

 -----------------------
 Carlos Santander Bernal

Jun 18 2004
parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Matthew" <admin stlsoft.dot.dot.dot.dot.org> escribió en el mensaje
news:cavmas$316d$1 digitaldaemon.com
| In STL it's called an asymmetric range, with the notation [from,to). The )
| represents that it's one past the end.
|

Understandable, but ambiguous. (from, to] is also asymmetric. (not too
serious, btw)
Anyway, that'd be only in the docs, right? Because there's no way to use
that notation in D, is there?

-----------------------
Carlos Santander Bernal
Jun 18 2004