On 11/15/10 7:31 AM, flyinghearts wrote:
Page 8, in the example:
import std.stdio, std.string;
void main()
{
uint[string] dictionary;
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 newlD = dictionary.length;
dictionary[word] = newlD;
writeln(newlD,word);
//Notice: the following line is not exist in the book.
//foreach(k, v; dictionary) writeln(k, " ", dictionary[k]);
}
}
}
There is a buffer reuse problem. The program does not work rightly on my
machine(win xp, DMD 2.050). Moreover, an error, "object.Error: Access
Violation", may occur when the associate array is directly accessed. It can be
tested just by uncommenting the line begin with "//foreach", and setting the
input data as:
a b
b c
The solution to the problem is:
dictionary[word] -> dictionary[word.idup]
or:
strip(line) -> strip(line.dup)
dmd bug, fixed recently.
http://d.puremagic.com/issues/show_bug.cgi?id=2954
Andrei