## D - Array slicing Associative and Rectangular arrays?

• Russ Lewis (18/18) Feb 18 2002 * Can you array slice associative arrays? IMHO, this is an almost
• Pavel Minayev (10/23) Feb 18 2002 How exactly do you slice an associative array? With item indices?
• Russ Lewis (24/48) Feb 18 2002 This is kind of what I'm thinking about:
• Pavel Minayev (8/15) Feb 18 2002 that
• Walter (6/10) Feb 20 2002 Correct, there is not a defined order to the values, and they are not
• Pavel Minayev (6/10) Feb 20 2002 I'd still request some form of loop to scan through the entire
• Russell Borogove (3/17) Feb 20 2002 Seconded.
• Russ Lewis (8/18) Feb 21 2002 Is the result of strcmp() defined or implementation dependent? If the
• Pavel Minayev (10/12) Feb 21 2002 Don't forget that D associative arrays don't necessary have string
• Russ Lewis (14/26) Feb 21 2002 Great! But I didn't know that...and I don't see it in the online docs. ...
• Karl Bochert (6/8) Feb 21 2002 Is that the second through fourth elements of start?
• Pavel Minayev (5/12) Feb 21 2002 Second through fourth (counting from 1).
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```* Can you array slice associative arrays?  IMHO, this is an almost
must-have feature...but be careful, Pavel (playful grin), because it
won't look right to have end-exclusive syntax here, either
* What happens if you array slice a rectangular array and try to set it
"by reference" to another array?
int[5][5] start = ....;
int[3][3] copyByValue,copyByReference;

copyByValue[] = start[1..4][1..4];    // doesn't seem like too much
trouble,
// copies the
values over
copyByReference = start[1..4][1..4];    // how in the world do you
do this?
--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
```
Feb 18 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message

* Can you array slice associative arrays?  IMHO, this is an almost
must-have feature...but be careful, Pavel (playful grin), because it
won't look right to have end-exclusive syntax here, either

How exactly do you slice an associative array? With item indices?
Then the same rules would apply as for normal arrays, including
end-exclusive syntax...

* What happens if you array slice a rectangular array and try to set it
"by reference" to another array?
int[5][5] start = ....;
int[3][3] copyByValue,copyByReference;

copyByValue[] = start[1..4][1..4];    // doesn't seem like too much
trouble,
// copies the
values over
copyByReference = start[1..4][1..4];    // how in the world do you
do this?

I believe there's no such thing as "rectangular slice" in D. start[1..4]
is an array of 3 elements of type int[5]. Now you slice it once again
with [1..4] and get an out-of-range exception because there's no element
with index 3. You could, however, slice it as start[1..4][1..3], which is
absolutely the same as start[2..3].
```
Feb 18 2002
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```Pavel Minayev wrote:

"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message

* Can you array slice associative arrays?  IMHO, this is an almost
must-have feature...but be careful, Pavel (playful grin), because it
won't look right to have end-exclusive syntax here, either

How exactly do you slice an associative array? With item indices?
Then the same rules would apply as for normal arrays, including
end-exclusive syntax...

This is kind of what I'm thinking about:

class Book {...};

Book[] booksOnShelf;
...

Book[] booksToCheckOut = booksOnShelf["Anne of Green Gables" .. "Farenheit
451"];

You see, I'm trying a slice here to include the range of books, assuming that
this associative array was indexed by title.  I think this feature would be
very useful for people using associative arrays.

But presumably, I want to make sure that I get my copy of "Farenheit 451"
(but nothing after it).  How do I do that?  You can't really do +1 to
string.  Of course, I could include up to "G" and then cut the extras off of
the tail, but then the usefulness of slicing is greatly diminished.

Today, my record on questions relating to arrays is about 0-2, so maybe
there's some easy way to do this that I've missed (smirk)

* What happens if you array slice a rectangular array and try to set it
"by reference" to another array?
int[5][5] start = ....;
int[3][3] copyByValue,copyByReference;

copyByValue[] = start[1..4][1..4];    // doesn't seem like too much
trouble,
// copies the
values over
copyByReference = start[1..4][1..4];    // how in the world do you
do this?

I believe there's no such thing as "rectangular slice" in D. start[1..4]
is an array of 3 elements of type int[5]. Now you slice it once again
with [1..4] and get an out-of-range exception because there's no element
with index 3. You could, however, slice it as start[1..4][1..3], which is
absolutely the same as start[2..3].

d'oh!  You're right, of course.  The "array slicing operator" returns an
array with as many dimensions as it started with...so rectangular slicing
isn't even an issue.  Do it with a for loop :p

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
```
Feb 18 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3C713B38.3099017D deming-os.org...

Book[] booksOnShelf;
...

Book[] booksToCheckOut = booksOnShelf["Anne of Green Gables" .. "Farenheit
451"];

You see, I'm trying a slice here to include the range of books, assuming

that
this associative array was indexed by title.  I think this feature would

be
very useful for people using associative arrays.

The key word here is "indexed". I'm not sure that D associative arrays are
indexed - in fact, I'm not sure that there's _any_ strictly defined
order of keys. As Walter has said before, the exact implementation of
associative array is left to the concrete compiler.
```
Feb 18 2002
"Walter" <walter digitalmars.com> writes:
```"Pavel Minayev" <evilone omen.ru> wrote in message
news:a4rij2\$26n8\$1 digitaldaemon.com...
The key word here is "indexed". I'm not sure that D associative arrays are
indexed - in fact, I'm not sure that there's _any_ strictly defined
order of keys. As Walter has said before, the exact implementation of
associative array is left to the concrete compiler.

Correct, there is not a defined order to the values, and they are not
"indexed" in the sense of looping through the members of an array by an
index.  The way to enumerate all the members is by converting the
associative array to a dynamic array.
```
Feb 20 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Walter" <walter digitalmars.com> wrote in message
news:a4vpei\$ng8\$2 digitaldaemon.com...

Correct, there is not a defined order to the values, and they are not
"indexed" in the sense of looping through the members of an array by an
index.  The way to enumerate all the members is by converting the
associative array to a dynamic array.

I'd still request some form of loop to scan through the entire
associative array without converting it to dynamic key/value arrays.
Let the order be "implementation-defined", there are many situations
where it just doesn't matter.
```
Feb 20 2002
Russell Borogove <kaleja estarcion.com> writes:
```Pavel Minayev wrote:
"Walter" <walter digitalmars.com> wrote in message
news:a4vpei\$ng8\$2 digitaldaemon.com...

Correct, there is not a defined order to the values, and they are not
"indexed" in the sense of looping through the members of an array by an
index.  The way to enumerate all the members is by converting the
associative array to a dynamic array.

I'd still request some form of loop to scan through the entire
associative array without converting it to dynamic key/value arrays.
Let the order be "implementation-defined", there are many situations
where it just doesn't matter.

Seconded.

-RB
```
Feb 20 2002
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```Pavel Minayev wrote:

"Walter" <walter digitalmars.com> wrote in message
news:a4vpei\$ng8\$2 digitaldaemon.com...

Correct, there is not a defined order to the values, and they are not
"indexed" in the sense of looping through the members of an array by an
index.  The way to enumerate all the members is by converting the
associative array to a dynamic array.

I'd still request some form of loop to scan through the entire
associative array without converting it to dynamic key/value arrays.
Let the order be "implementation-defined", there are many situations
where it just doesn't matter.

Is the result of strcmp() defined or implementation dependent?  If the
former, then strcmp()'s comparison algorithm would work fine for me....

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
```
Feb 21 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3C75179F.EF2FE1BF deming-os.org...

Is the result of strcmp() defined or implementation dependent?  If the
former, then strcmp()'s comparison algorithm would work fine for me....

Don't forget that D associative arrays don't necessary have string
keys. It's perfectly valid to have an int, float, or even Object
as a key.

Also, any "comparison algorithm" sorta limits the implementation -
it either has to store the keys in that order in memory, or the
iteration process will be quite slow (because a look-up is performed
for each key in the table). How do you implement an ordered scan
for a hash table, for example?
```
Feb 21 2002
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```Pavel Minayev wrote:

"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3C75179F.EF2FE1BF deming-os.org...

Is the result of strcmp() defined or implementation dependent?  If the
former, then strcmp()'s comparison algorithm would work fine for me....

Don't forget that D associative arrays don't necessary have string
keys. It's perfectly valid to have an int, float, or even Object
as a key.

Great!  But I didn't know that...and I don't see it in the online docs.  The
closest I see is a statement that "Associated arrays are supported for all
following types", which is hard to understand, but I guessed meant "you can
have an associative array (keyed by strings) of any type".

Also, any "comparison algorithm" sorta limits the implementation -
it either has to store the keys in that order in memory, or the
iteration process will be quite slow (because a look-up is performed
for each key in the table). How do you implement an ordered scan
for a hash table, for example?

Right.  It would be nice to be able to slice these arrays with compiler
support in the language.  I don't really care the underlying algorithm; I was
just hoping that I could have access to the easy functionality.  However if
more than strings are supported, then it gets hard.

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
```
Feb 21 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3C753CF5.D4313464 deming-os.org...

Great!  But I didn't know that...and I don't see it in the online docs.

The
closest I see is a statement that "Associated arrays are supported for all
following types", which is hard to understand, but I guessed meant "you

can
have an associative array (keyed by strings) of any type".

Just remember the syntax:

int[char[]] foo;       // keys are char[]
char[][double] bar;    // keys are double
Object[Object] baz;    // keys are Object

The only thing is that currently DMD alpha only supports int[char[]] =)
I guess this also includes pointers since they're 32-bit as well
(or am I wrong, Walter?).
```
Feb 21 2002
Karl Bochert <kbochert ix.netcom.com> writes:
``` I believe there's no such thing as "rectangular slice" in D. start[1..4]
is an array of 3 elements of type int[5].

Is that the second through fourth elements of start?
Or the first through third?
Neither  seems to be what [1..4] is saying!
[1..4] clearly shows either a 1 and a 4  or  a 'first' and a 'fourth'
inside the brackets (array).

Karl Bochert
```
Feb 21 2002
"Pavel Minayev" <evilone omen.ru> writes:
```"Karl Bochert" <kbochert ix.netcom.com> wrote in message
news:1105_1014316952 bose...
I believe there's no such thing as "rectangular slice" in D. start[1..4]
is an array of 3 elements of type int[5].

Is that the second through fourth elements of start?
Or the first through third?

Second through fourth (counting from 1).

Neither  seems to be what [1..4] is saying!
[1..4] clearly shows either a 1 and a 4  or  a 'first' and a 'fourth'
inside the brackets (array).

C (and D) arrays are zero-based, you know =)
So the element isn't actually 'first', but 'zeroth'.
```
Feb 21 2002