@@ -484,20 +484,23 @@ $(P Properties for associative arrays are:)
484
484
else evaluates the $(D create) delegate and adds it to the associative array))
485
485
)
486
486
487
- $(HR)
487
+ $(H2 $(LNAME2 examples, Examples))
488
+
488
489
$(H3 $(LNAME2 aa_example, Associative Array Example: word count))
489
490
490
491
$(P Let's consider the file is ASCII encoded with LF EOL.
491
492
In general case we should use $(I dchar c) for iteration
492
493
over code points and functions from $(LINK2 $(ROOT_DIR)phobos/std_uni.html,std.uni).
493
494
)
494
495
496
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
495
497
---------
498
+ import std.algorithm : sort;
496
499
import std.file; // D file I/O
497
500
import std.stdio;
498
501
import std.ascii;
499
502
500
- void main (string[] args)
503
+ void main(string[] args)
501
504
{
502
505
ulong totalWords, totalLines, totalChars;
503
506
ulong[string] dictionary;
@@ -507,7 +510,7 @@ $(H3 $(LNAME2 aa_example, Associative Array Example: word count))
507
510
{
508
511
ulong wordCount, lineCount, charCount;
509
512
510
- foreach(line; File(arg).byLine())
513
+ foreach (line; File(arg).byLine())
511
514
{
512
515
bool inWord;
513
516
size_t wordStart;
@@ -552,19 +555,21 @@ $(H3 $(LNAME2 aa_example, Associative Array Example: word count))
552
555
totalChars += charCount;
553
556
}
554
557
558
+ const char[37] hr = '-';
555
559
if (args.length > 2)
556
560
{
557
- writefln("-------------------------------------\n%8s%8s%8s total",
558
- totalLines, totalWords, totalChars);
561
+ writeln(hr);
562
+ writefln("%8s%8s%8s total", totalLines, totalWords, totalChars);
559
563
}
560
564
561
- writeln("-------------------------------------" );
565
+ writeln(hr );
562
566
foreach (word; dictionary.keys.sort)
563
567
{
564
568
writefln("%3s %s", dictionary[word], word);
565
569
}
566
570
}
567
571
---------
572
+ )
568
573
569
574
$(H3 $(LNAME2 aa_example_iteration, Associative Array Example: counting pairs))
570
575
0 commit comments