March 10, 2005 Re: The new '$' has an inconsistent behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to David L. Davis | David L. Davis wrote: <snip> > Please do not be offended if you already know the following. But someone here > on the forum pointed out to me, that this is the way D deals with the length > when it’s doing array slicing. In other words sTest.length equals 26 which is > based on 1 starting in the first position. Length is length. Is has nothing whatsoever to do with how elements are numbered. Suppose you have a row of items numbered like this 0 1 2 3 4 5 6 7 8 9 There are ten of them. Now suppose they're numbered like this 1 2 3 4 5 6 7 8 9 10 There are still ten of them. Now suppose they're numbered 3 4 5 6 7 8 9 10 11 12 Here, there are ten again. The length of an array is defined as the number of elements in the array. Not by the index of the last element by any interpretation. > Where as slicing [0..sTest.length] is > based on zero starting in the first position, thus in this special case with a > little D magic (hack)… both “sTest.length” and “sTest.length – 1” end up > displaying same result. I'll have to try it and see when I get home. If it does as you describe, then it's a bug in DMD. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
March 10, 2005 Finally: The new '$' has an NO inconsistent behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Thanks Stewart, your words restored my faith in the world. (I almost started to write a very long explanation about counting simple thing) D uses 0 based array indexing. There are no sign to have any bugs in that. Anybody could complain about the fact, that the indexing IS 0 based, but not about, how could it be done. It's elementary mathematics. >Length is length. Is has nothing whatsoever to do with how elements are numbered. Question (for $1000): If there are 5 tram stations and you want to travel along the whole line, how many times will the tram start or how many stations will you pass? 4, no question. This is how our human languages are "accurate". Programming languages can not be so imperfect. (Remember the array indexbase problems in Visual Basic: Ubound, Lbound, etc.) That's all. Tamas Nagy >> little D magic (hack)… both “sTest.length” and “sTest.length – 1” end up displaying same result. >I'll have to try it and see when I get home. If it does as you describe, then it's a bug in DMD. You don't need. I did it. import std.stdio; void main(char[][] args) { char[] tiz="0123456789"; char[] husz="01234567890123456789"; printf("tiz=%.*s\n",tiz); printf("tiz[]=%.*s\n",tiz[]); printf("tiz[0..tiz.length=%.*s\n",tiz[0..tiz.length]); printf("tiz[0..$]=%.*s\n",tiz[0..$]); printf("tiz[0..10]=%.*s\n",tiz[0..10]); printf("tiz[3..tiz.length-3]=%.*s\n",tiz[3..tiz.length-3]); printf("tiz[3..$-3]=%.*s\n",tiz[3..$-3]); printf("tiz[3..7]=%.*s\n",tiz[3..7]); printf("\n"); printf("tiz[0..9]=%.*s\n",tiz[0..9]); printf("tiz[0..$-1]=%.*s\n",tiz[0..$-1]); printf("\n"); printf("husz=%.*s\n",husz); printf("husz[]=%.*s\n",husz[]); printf("husz[0..husz.length]=%.*s\n",husz[0..husz.length]); printf("husz[0..$]=%.*s\n",husz[0..$]); printf("husz[0..20]=%.*s\n",husz[0..20]); printf("husz[3..husz.length-3]=%.*s\n",husz[3..husz.length-3]); printf("husz[3..$-3]=%.*s\n",husz[3..$-3]); printf("husz[3..17]=%.*s\n",husz[3..17]); printf("\n"); printf("husz[0..19]=%.*s\n",husz[0..19]); printf("husz[0..$-1]=%.*s\n",husz[0..$-1]); } Result: tiz=0123456789 tiz[]=0123456789 tiz[0..tiz.length=0123456789 tiz[0..$]=0123456789 tiz[0..10]=0123456789 tiz[3..tiz.length-3]=3456 tiz[3..$-3]=3456 tiz[3..7]=3456 tiz[0..9]=012345678 tiz[0..$-1]=012345678 husz=01234567890123456789 husz[]=01234567890123456789 husz[0..husz.length]=01234567890123456789 husz[0..$]=01234567890123456789 husz[0..20]=01234567890123456789 husz[3..husz.length-3]=34567890123456 husz[3..$-3]=34567890123456 husz[3..17]=34567890123456 husz[0..19]=0123456789012345678 husz[0..$-1]=0123456789012345678 |
March 10, 2005 Re: The new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | > Don't be so childish. It's not a question of having something to prove, or at least not in the way you mean. The point I was tryint to make is that it seems like there are people here who are constantly trying to trip others up on the most minor of logical errors, and to put it bluntly, it's stupid. Sorry if I expressed an opinion without anything to back it up, I wasn't aware that it was such a dire mistake on my part. I thought Walter was testing this $ out, and would rather have peoples' opinions about it rather than constant bickering about whose logic is faulty. > If you want people to diligently read your posts when they contain unsubstantiated opinion, you're living in a dream world. Any people who are interested in such are *far* more likely to watch Fox News. Oh good, throw in some opinionated blanket political statements. You're a real charmer. |
March 10, 2005 Re: The new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >> If you want people to diligently read your posts when they contain unsubstantiated opinion, you're living in a dream world. Any people who are interested in such are *far* more likely to watch Fox News.
>
> Oh good, throw in some opinionated blanket political statements. You're a real charmer.
Yes. Dropping in that was not sensible, given the insane level of political sensibility in the world at the moment. (Nor does it necessarily reflect any political persuasion I might hold.) Consider it sheepishly retracted.
|
March 13, 2005 Re: Another way | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji Smith | On Wed, 09 Mar 2005 19:19:48 -0700, Benji Smith <dlanguage@xxagg.com> wrote:
> ...or, in cases where the array has been poorly_named_to_begin_with,
> you could do something like this...
>
> int len = idioticly_long_variable_name.length;
> int[] slice = idioticly_long_variable_name[x .. len];
>
> It's slightly less syntactically-sugary than using "length" without a
> prefix, or using a "$" token, but the headaches avoided by doing so
> would be worth it, in my opinion.
Just remember 'len' could, in certain situations, become stale before use. Not in the above example, but in slightly more complex code, eg.
int len = array.length;
if (foo(array)) { <- foo modifies array.length
writef("%s"array[5..len]); <- len is actually > or < than array.length here
}
Regan
|
March 13, 2005 Re: Another way | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: >> int len = idioticly_long_variable_name.length; >> int[] slice = idioticly_long_variable_name[x .. len]; > Just remember 'len' could, in certain situations, become stale before use. Not in the above example, but in slightly more complex code, eg. > > int len = array.length; > if (foo(array)) { <- foo modifies array.length > writef("%s"array[5..len]); <- len is actually > or < than array.length here > } But *only* if array is declared with an "out" parameter... (and those are "dangerous" even with int and char and so ?) > import std.stdio; > > void foo(char[] str) > { > str ~= "X"; > } > > void bar(inout char[] str) > { > str ~= "X"; > } > > void main() > { > char[] str = "test"; > writefln("str: %s", str); > > foo(str); > writefln("foo: %s", str); > > bar(str); > writefln("bar: %s", str); > } str: test foo: test bar: testX Note how changing the slice declared as "in" did *not* do anything. (except if you modify the actual characters, another discussion...) --anders |
March 14, 2005 Re: Another way | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Sun, 13 Mar 2005 10:42:26 +0100, Anders F Björklund <afb@algonet.se> wrote:
> Regan Heath wrote:
>
>>> int len = idioticly_long_variable_name.length;
>>> int[] slice = idioticly_long_variable_name[x .. len];
>
>> Just remember 'len' could, in certain situations, become stale before use. Not in the above example, but in slightly more complex code, eg.
>> int len = array.length;
>> if (foo(array)) { <- foo modifies array.length
>> writef("%s"array[5..len]); <- len is actually > or < than array.length here
>> }
>
> But *only* if array is declared with an "out" parameter...
> (and those are "dangerous" even with int and char and so ?)
True. Thanks.
Regan
|
April 10, 2005 Re: Finally: The new '$' has an NO inconsistent behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to MicroWizard | This whole argument stems from a weird array slicing interval. It makes sense once you play with it. However, To begin with it's confusing. In set builder notation an array slice is such: array[ a .. b ]; Is there elements Array sub N where N is integers belonging to the interval [a, b) So it starts as a closed interval, and ends as an open one. This is rather counterintuitive, though useful. One would expect it to be INCLUSIVE on both sides. In article <d0qd17$n4$1@digitaldaemon.com>, MicroWizard says... > >Thanks Stewart, your words restored my faith in the world. (I almost started to write a very long explanation about counting simple thing) > >D uses 0 based array indexing. There are no sign to have any bugs in that. >Anybody could complain about the fact, that the indexing IS 0 based, >but not about, how could it be done. It's elementary mathematics. > >>Length is length. Is has nothing whatsoever to do with how elements are numbered. > >Question (for $1000): >If there are 5 tram stations and you want to travel along the whole line, >how many times will the tram start or how many stations will you pass? > >4, no question. This is how our human languages are "accurate". >Programming languages can not be so imperfect. >(Remember the array indexbase problems in Visual Basic: Ubound, Lbound, etc.) > >That's all. > >Tamas Nagy > >>> little D magic (hack)… both “sTest.length” and “sTest.length – 1” end up displaying same result. > >>I'll have to try it and see when I get home. If it does as you describe, then it's a bug in DMD. > >You don't need. I did it. > >import std.stdio; > >void main(char[][] args) >{ >char[] tiz="0123456789"; >char[] husz="01234567890123456789"; > >printf("tiz=%.*s\n",tiz); >printf("tiz[]=%.*s\n",tiz[]); >printf("tiz[0..tiz.length=%.*s\n",tiz[0..tiz.length]); >printf("tiz[0..$]=%.*s\n",tiz[0..$]); >printf("tiz[0..10]=%.*s\n",tiz[0..10]); >printf("tiz[3..tiz.length-3]=%.*s\n",tiz[3..tiz.length-3]); >printf("tiz[3..$-3]=%.*s\n",tiz[3..$-3]); >printf("tiz[3..7]=%.*s\n",tiz[3..7]); >printf("\n"); >printf("tiz[0..9]=%.*s\n",tiz[0..9]); >printf("tiz[0..$-1]=%.*s\n",tiz[0..$-1]); >printf("\n"); > >printf("husz=%.*s\n",husz); >printf("husz[]=%.*s\n",husz[]); >printf("husz[0..husz.length]=%.*s\n",husz[0..husz.length]); >printf("husz[0..$]=%.*s\n",husz[0..$]); >printf("husz[0..20]=%.*s\n",husz[0..20]); >printf("husz[3..husz.length-3]=%.*s\n",husz[3..husz.length-3]); >printf("husz[3..$-3]=%.*s\n",husz[3..$-3]); >printf("husz[3..17]=%.*s\n",husz[3..17]); >printf("\n"); >printf("husz[0..19]=%.*s\n",husz[0..19]); >printf("husz[0..$-1]=%.*s\n",husz[0..$-1]); >} > >Result: >tiz=0123456789 >tiz[]=0123456789 >tiz[0..tiz.length=0123456789 >tiz[0..$]=0123456789 >tiz[0..10]=0123456789 >tiz[3..tiz.length-3]=3456 >tiz[3..$-3]=3456 >tiz[3..7]=3456 > >tiz[0..9]=012345678 >tiz[0..$-1]=012345678 > >husz=01234567890123456789 >husz[]=01234567890123456789 >husz[0..husz.length]=01234567890123456789 >husz[0..$]=01234567890123456789 >husz[0..20]=01234567890123456789 >husz[3..husz.length-3]=34567890123456 >husz[3..$-3]=34567890123456 >husz[3..17]=34567890123456 > >husz[0..19]=0123456789012345678 husz[0..$-1]=0123456789012345678 > > |
Copyright © 1999-2021 by the D Language Foundation