string comparison

doubleagent doubleagent03 at gmail.com
Mon Dec 20 16:45:20 PST 2010


> Okay. I don't know what the actual code looks like

Here.

import std.stdio, std.string;

void main() {
        uint[string] dictionary; // v[k], so string->uint
        foreach (line; stdin.byLine()) {
                // break sentence into words
                // Add each word in the sentence to the vocabulary
                foreach (word; splitter(strip(line))) {
                        if (word in dictionary) continue; // nothing to do
                        auto newId = dictionary.length;
                        dictionary[word] = newId;
                        writefln("%s\t%s", newId, word);
                }
        }
}

> ...

Okay, suppose you're right.  The behavior is still incorrect because the
associative array has allowed two identical keys...identical because the only
difference between two strings which I care about are the contents of their
character arrays.

> Also, it
> would be _really_ annoying to have to mark variables mutable all over the place
> as you would inevitably have to do.

Obviously your other points are valid, but I haven't found this to be true
(Clojure is pure joy).  Maybe you're right because D is a systems language and
mutability needs to be preferred, however after only a day or two of exposure to
this language that assumption also appears to be wrong.  Take a look at Walter's
first attempted patch to bug 2954: 13 lines altered to explicitly include
immutable, and 4 altered to treat variables as const:
http://www.dsource.org/projects/dmd/changeset/749

But I'm willing to admit that my exposure is limited, and that particular example
is a little biased.


More information about the Digitalmars-d-learn mailing list