forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc.dd
75 lines (59 loc) · 1.5 KB
/
wc.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Ddoc
$(D_S Example: wc,
$(P This program is the D version of the classic `wc` (wordcount) C
program.
It serves to demonstrate how to read files, slice arrays,
and simple symbol table management with associative arrays.
)
$(RUNNABLE_EXAMPLE
----
import std.stdio;
import std.algorithm;
void main(string[] args)
{
ulong wordCount;
ulong lineCount;
ulong charCount;
int[string] dictionary;
writeln(" lines words bytes file");
foreach (arg; args[1 .. $])
{
ulong lWordCount;
ulong lCharCount;
ulong lLineCount;
auto file = File(arg);
foreach (line; file.byLine(KeepTerminator.yes))
{
lCharCount += line.length;
foreach (word; splitter(line))
{
lWordCount += 1;
if (auto count = word in dictionary)
*count += 1;
else
dictionary[word.idup] = 1;
}
lLineCount += 1;
}
writefln("%8s%8s%8s %s\n", lLineCount, lWordCount, lCharCount, arg);
wordCount += lWordCount;
lineCount += lLineCount;
charCount += lCharCount;
}
const char[37] hr = '-';
if (args.length > 2)
{
writeln(hr);
writefln("%8s%8s%8s total", lineCount, wordCount, charCount);
}
writeln(hr);
foreach (word; sort(dictionary.keys))
{
writefln("%3s %s", dictionary[word], word);
}
}
----
)
)
Macros:
TITLE=Word Count