D 中基于关联数组的排序

发布于 2024-07-30 06:12:22 字数 1358 浏览 2 评论 0 原文

我正在尝试遵循不同地方给出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2024-08-06 06:12:22

尝试将其添加到文件顶部附近:

import std.algorithm;

Try adding this near the top of the file:

import std.algorithm;
扮仙女 2024-08-06 06:12:22

这是一种更简单的方法来获取输入文件(从 cmdline),获取行/单词并按降序打印单词频率表:

import std.algorithm;
import std.file;
import std.stdio;
import std.string;

void main(string[] args)
{   
    auto contents = cast(string)read(args[1]);
    uint[string] freqs;

    foreach(i,line; splitLines(contents))
        foreach(word; split(strip(line)))
            ++freqs[word];

    string[] words = freqs.keys;
    sort!((a,b)=> freqs[a]>freqs[b])(words);

    foreach(s;words) 
        writefln("%s\t\t%s",s,freqs[s]);
}

嗯,差不多 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 :

import std.algorithm;
import std.file;
import std.stdio;
import std.string;

void main(string[] args)
{   
    auto contents = cast(string)read(args[1]);
    uint[string] freqs;

    foreach(i,line; splitLines(contents))
        foreach(word; split(strip(line)))
            ++freqs[word];

    string[] words = freqs.keys;
    sort!((a,b)=> freqs[a]>freqs[b])(words);

    foreach(s;words) 
        writefln("%s\t\t%s",s,freqs[s]);
}

Well, almost 4 years later... :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文