One more update on d-programming-language.org

Mafi mafi at example.org
Wed Sep 15 04:27:23 PDT 2010


Am 15.09.2010 12:33, schrieb bearophile:
> Seth Hoenig:
>> [1] http://live.gnome.org/Vala/ValaForJavaProgrammers
>> http://live.gnome.org/Vala/ValaForCSharpProgrammers
>
> I will read those pages.
> On the D site there are pages about D1 for C/C++ programmers. I will eventually write a page about D2 for Python programmers.
> In the meantime:
> http://tinyurl.com/2d44sx7
>
> Bye,
> bearophile

I don't know python very well, but I think the article is interesting 
anyways but what I don't get is this:

 >Python indexes can be negative, to wrap around:
 >
 >s = "abcdefg"
 >assert s[-2] == 'f'
 >
 >
 >But D doesn't allow that, you need to use $:
 >
 >auto s = "abcdefg";
 >assert(s[$-2] == 'f');
 >
 >
 >Here the -2 is a constant known at compile-time. If it's a run-time 
 >variable then you need more complex code, like:
 >
 >void main() {
 >    auto s = "abcdefg";
 >
 >    int index = -2;
 >    char c = (index < 0) ? s[$+index] : s[index];
 >    assert(c == 'f');
 >
 >    index = 3;
 >    c = (index < 0) ? s[$+index] : s[index];
 >    assert(c == 'd');
 >}
 >
 >
 >And you need to be careful to use a signed value for the index.

Maybe it's a silly question but what's wrong with
s[($+i)%$] //Wow, looks like Perl

Look
for i=(-1) and $=5 it's (5+(-1))%5 = 4 % 5 = 4
for i=2    and $=5 it's (5+2)   %5 = 7 % 5 = 2

Mafi


More information about the Digitalmars-d mailing list