Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 19, 2001 array slicing ranges | ||||
---|---|---|---|---|
| ||||
From The D Programming Language, arrays section: Is this last bit about b = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] ? In other words, are ranges inclusive, or not? Sean Slicing Slicing an array means to specify a subarray of it. For example: int a[10]; declare array of 10 ints int b[]; b = a[1..3]; a[1..3] is a 3 element array consisting of a[1], a[2], and a[3] The [] is shorthand for a slice of the entire array. For example, the assignments to b: int a[10]; int b[] b = a; b = a[]; b = a[0 .. a.length]; are all semantically equivalent. |
November 19, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tai22$1fok$1@digitaldaemon.com... > From The D Programming Language, arrays section: Is this last bit about b > = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] ? In other words, are ranges inclusive, or not? "s[0..4] = ... // error, only 3 elements in s" So ranges are inclusive, and you get an exception if you write s[0..s.length]. |
November 19, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tai22$1fok$1@digitaldaemon.com... > From The D Programming Language, arrays section: Is this last bit about b > = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] a[0..a.length] is it. |
November 19, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 8-0 This is a Bad Thing, IMHO. The .length property should give the *length* of the array, not the maximum index! You could add a maximum index property, though: b = a[0..a.length-1]; // ok b = a[0..a.length]; // ArrayOutOfBoundsException b = a[0..a.maxIndex]; // ok Or, perhaps better yet, you could just allow unbounded slicing, which goes to the edges: b = a[0..]; b = a[..a.maxIndex]; b = a[..]; // all 3 of these are identical Walter wrote: > "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tai22$1fok$1@digitaldaemon.com... > > From The D Programming Language, arrays section: Is this last bit about > b > > = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] > > a[0..a.length] is it. -- 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))) ] |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | But the length is not the maximum index. "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BF94B93.6D0C72FF@deming-os.org... > 8-0 > > This is a Bad Thing, IMHO. The .length property should give the *length* of the > array, not the maximum index! You could add a maximum index property, though: > > b = a[0..a.length-1]; // ok > b = a[0..a.length]; // ArrayOutOfBoundsException > b = a[0..a.maxIndex]; // ok > > Or, perhaps better yet, you could just allow unbounded slicing, which goes to > the edges: > > b = a[0..]; > b = a[..a.maxIndex]; > b = a[..]; // all 3 of these are identical > > > > Walter wrote: > > > "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tai22$1fok$1@digitaldaemon.com... > > > From The D Programming Language, arrays section: Is this last bit about > > b > > > = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] > > > > a[0..a.length] is it. > > -- > 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))) ] > > |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:9tbc2a$1vm1$1@digitaldaemon.com... > "Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tai22$1fok$1@digitaldaemon.com... > > From The D Programming Language, arrays section: Is this last bit about > b > > = a[0 .. a.length] true, or should it actually be b = a[0 .. a.length-1] > > ? In other words, are ranges inclusive, or not? > > "s[0..4] = ... // error, only 3 elements in s" > > So ranges are inclusive, and you get an exception if > you write s[0..s.length]. s[0..4] has 4 elements: s[0], s[1], s[2], s[3] |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter |
Walter wrote:
>
> s[0..4] has 4 elements: s[0], s[1], s[2], s[3]
So array-slice ranges are half-open, yes? Your first slice example in the spec says:
b = a[1..3]; a[1..3] is a 3 element array consisting of
a[1], a[2], and a[3]
Should this read "a 2 element array consisting of a[1] and a[2]"?
This kind of ranging is going to piss off C programmers; this thread being case in point.
-RB
|
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Borogove | "Russell Borogove" <kaleja@estarcion.com> wrote in message news:3BF9CD48.E638FD8@estarcion.com... > > > Walter wrote: > > > > s[0..4] has 4 elements: s[0], s[1], s[2], s[3] > > So array-slice ranges are half-open, yes? Your first slice example in the spec says: > > b = a[1..3]; a[1..3] is a 3 element array consisting of a[1], a[2], and a[3] Looks like the spec is wrong <g>. > This kind of ranging is going to piss off C programmers; this thread being case in point. First I have to get the documentation right. |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:9tche0$2n2v$3@digitaldaemon.com... > But the length is not the maximum index. Then [0..length] is not legal. |
November 20, 2001 Re: array slicing ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:9tcv3f$2vca$1@digitaldaemon.com... > "Walter" <walter@digitalmars.com> wrote in message news:9tche0$2n2v$3@digitaldaemon.com... > > > But the length is not the maximum index. > > Then [0..length] is not legal. I don't understand what you're driving at, then. |
Copyright © 1999-2021 by the D Language Foundation