Problem with AAs and ?:

bearophile bearophileHUGS at lycos.com
Sat Mar 8 07:01:13 PST 2008


Can someone explain me where such difference comes from?
Is this a bug?

import std.stdio: writefln;

void main() {
    string[] words = ["how", "are", "you", "are"];

    int[string] aa1;
    foreach (w; words)
        aa1[w] = ((w in aa1) ? (aa1[w] + 1) : 2);
    writefln(aa1); // Prints: [how:1,you:1,are:2]

    int[string] aa2;
    foreach (w; words)
        if (w in aa2)
            aa2[w]++;
        else
            aa2[w] = 2;
    writefln(aa2); // Prints: [how:2,you:2,are:3]
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list