[Issue 11507] New: Associative Array Documentation

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 12 15:31:14 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11507

           Summary: Associative Array Documentation
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: websites
        AssignedTo: nobody at puremagic.com
        ReportedBy: bugzilla at digitalmars.com
                CC: adamw at prospectivesoftware.com


--- Comment #0 from Walter Bright <bugzilla at digitalmars.com> 2013-11-12 15:31:09 PST ---
Chuck Allison writes:

It states:

When an AA indexing access appears on the left side of an assignment operator,
it is specially handled for setting AA entry associated with the key.

string[int] aa;
string s;
s = aa[1];          // throws RangeError in runtime
aa[1] = "hello";    // handled for setting AA entry
s = aa[1];          // succeeds to lookup
assert(s == "hello");


If the assigned value type is equivalent with the AA element type:

    If the indexing key does not yet exist in AA, a new AA entry will be
allocated, and it will be initialized with the assigned value.
    If the indexing key already exists in the AA, the setting runs normal
assignment.


It does not explicitly state that a new entry may be default initialized, as
in:

int[string] myaa;
++myaa[“foo”];   // 0+1 = 1

It might be nice to say so.

Also, I realize this is a matter of taste, but wouldn’t a short, high-level
word-count example be more attractive than the example that is there? For
example:

    string[] words = split(cast(string) read(filename));
    int[string] counts;
    foreach (word; words)
        ++counts[word];    // default initialization used here
    foreach (w; counts.keys.sort)
        writefln("%s: %d", w, counts[w]);

This code opens a file, tokenizes it, does the word mapping, and prints the
result in sorted order all in 6 lines. I find this more compelling and easier
to grok at a glance.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list