Key and value with ranges
Imperatorn
johan_forsberg_86 at hotmail.com
Mon Oct 2 05:44:45 UTC 2023
On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:
> ```d
> import std;
>
> auto data=“I went for a walk, and fell down a hole.”;
>
> void main(string[] args) {
> int[string] dic;
> struct WordCnt {
> string word;
> ulong count;
> string toString() const {
> return text("Word: ", word, " - number of
> instances: ", count);
> }
> }
> WordCnt[] wc;
> data
> .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c :
> ' ')
> .to!string
> .splitter
> .each!(d => dic[d]+=1);
> foreach(key, value; dic)
> wc~=WordCnt(key, value);
> wc.sort!"a.count>b.count".each!writeln;
> }
> ```
>
> How can I improve this code? Like avoiding using foreach.
You don't need a struct at all, you can just have an int[string]
aa
More information about the Digitalmars-d-learn
mailing list