www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11507] New: Associative Array Documentation

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: bugzilla digitalmars.com
                CC: adamw prospectivesoftware.com



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: -------
Nov 12 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11507


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc





     string[] words = split(cast(string) read(filename));
Better to use UFCS.
     foreach (w; counts.keys.sort)
Built-in sort is deprecated!! -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 12 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11507


Chuck Allison <chuck freshsources.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chuck freshsources.com



PST ---
Is this what you had in mind?

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

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