我目前正在学习 D 并且正在采取婴儿步骤,所以请多多包涵。
我正在阅读一本名为“The D Programming Language”的书。我正在为我的代码使用 D-IDE。我目前正在编写一个程序,如果这本书还没有单词,它应该将单词添加到它的词汇表(字典)中。
问题是,这本书提供的代码是无效的,而不是仅仅继续阅读结果应该是什么等等。我想尝试解决它。当然是一个我对 D 很陌生的问题。
代码如下所示:
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 newID = dictionary.length;
dictionary[word] = newID;
writeln(newID, '\t', word);
}
}
}
IDE 说Error: undefined identifier splitter
,由于我对 Java 非常有经验,我猜该错误意味着该方法不存在,因此它试图将其作为变量处理,但它也不存在。所以我尝试将其更改为“拆分”。这会在字典中产生另一个错误:Error: associative arrays can only be assigned values with immutable keys, not char[]
所以我真的不知道该怎么做才能解决这个问题并让它发挥作用。当本应教你的书籍中的代码不起作用时,真是令人沮丧。我正在使用 dmd2。