D 中基于关联数组的排序
我正在尝试遵循不同地方给出的 D 应用程序的示例。 一般来说,在学习语言时,我会从示例应用程序开始,然后自己更改它们,纯粹是为了测试东西。
引起我注意的一个应用程序是计算传入的文本块中单词的频率。由于字典是在关联数组中构建的(元素存储频率,键是单词本身),因此输出没有任何特定的顺序。 因此,我尝试根据网站上给出的示例对数组进行排序。
无论如何,该示例显示了 lambda 'sort!(...)(array);' 但是当我尝试代码 dmd 时不会编译它。
这是简化的代码:
import std.stdio;
import std.string;
void main() {
uint[string] freqs;
freqs["the"] = 51;
freqs["programming"] = 3;
freqs["hello"] = 10;
freqs["world"] = 10;
/*...You get the point...*/
//This is the actual example given, but it doesn't
//seem to work, old D version???
//string[] words = array(freqs.keys);
//This seemed to work
string[] words = freqs.keys;
//Example given for how to sort the 'words' array based on
//external criteria (i.e. the frequency of the words from
//another array). This is the line where the compilor craps out!
sort!((a,b) {return freqs[a] < freqs[b];})(words);
//Should output in frequency order now!
foreach(word; words) {
writefln("%s -> %s", word, freqs[word]);
}
}
当我尝试编译此代码时,我得到以下内容
s1.d(24): Error: undefined identifier sort s1.d(24): Error: function expected before (), not sort of type int
谁能告诉我我需要在这里做什么?
我使用 DMD v2.031,我尝试安装 gdc,但这似乎只支持 v1 语言规范。 我才开始研究 dil,所以我无法评论这是否支持上面的代码。
I am trying to follow examples given in various places for D apps. Generally when learning a language I start on example apps and change them myself, purely to test stuff out.
One app that caught my eye was to count the frequency of words in a block of text passed in. As the dictionary was built up in an associative array (with the elements storing the frequency, and the keys being the words themselves), the output was not in any particular order. So, I attempted to sort the array based on examples given on the site.
Anyway, the example showed a lambda 'sort!(...)(array);' but when I attempt the code dmd won't compile it.
Here's the boiled down code:
import std.stdio;
import std.string;
void main() {
uint[string] freqs;
freqs["the"] = 51;
freqs["programming"] = 3;
freqs["hello"] = 10;
freqs["world"] = 10;
/*...You get the point...*/
//This is the actual example given, but it doesn't
//seem to work, old D version???
//string[] words = array(freqs.keys);
//This seemed to work
string[] words = freqs.keys;
//Example given for how to sort the 'words' array based on
//external criteria (i.e. the frequency of the words from
//another array). This is the line where the compilor craps out!
sort!((a,b) {return freqs[a] < freqs[b];})(words);
//Should output in frequency order now!
foreach(word; words) {
writefln("%s -> %s", word, freqs[word]);
}
}
When I try to compile this code, I get the following
s1.d(24): Error: undefined identifier sort s1.d(24): Error: function expected before (), not sort of type int
Can anyone tell me what I need to do here?
I use DMD v2.031, I've tried installing the gdc but this only seems to support the v1 language spec. I've only started looking at dil, so I can't comment on whether this supports the code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其添加到文件顶部附近:
Try adding this near the top of the file:
这是一种更简单的方法来获取输入文件(从 cmdline),获取行/单词并按降序打印单词频率表:
嗯,差不多 4 年后......:-)
Here's an even simpler way to get an input file (from cmdline), get lines/words and print a table of word frequencing, in descending order :
Well, almost 4 years later... :-)