Thread overview
[Issue 9868] New: Hash iteration should support counter variable
Apr 03, 2013
Andrej Mitrovic
Apr 07, 2013
Martin Nowak
Apr 07, 2013
Andrej Mitrovic
April 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9868

           Summary: Hash iteration should support counter variable
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-03 12:10:58 PDT ---
void main()
{
    string[string] hash;
    foreach (i, key, val; hash)  // ng
    {
    }
}

I think it would be nice to support this, it allows for things like:

void main()
{
    string[string] hash = ["foo":"1", "bar":"2"];

    string res;
    foreach (i, key, val; hash)  // ng
    {
        if (i) res ~= ", ";
        res ~= key ~ " " ~ val;
    }

    assert(res == "foo 1, bar 2");
}

A map + joiner call would suffice here of course.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9868


Martin Nowak <code@dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@dawg.eu


--- Comment #1 from Martin Nowak <code@dawg.eu> 2013-04-06 20:33:15 PDT ---
The problem is there is no index in an unsorted map.
But using the known counter from array foreach makes it appear as if there was
a relation between i and key/value.
I don't think it's worth the possible confusion.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9868


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-07 04:24:03 PDT ---
Fair enough. Here's a library workaround when it's really needed:

----
import std.stdio;
import std.traits;

struct Walk(Hash)
    if (isAssociativeArray!Hash)
{
    Hash hash;

    int opApply(int delegate(size_t idx, KeyType!Hash key, ValueType!Hash val)
dg)
    {
        int result = 0;

        size_t idx;
        foreach (key, val; hash)
        {
            result = dg(idx++, key, val);
            if (result)
                break;
        }

        return result;
    }
}

auto walk(Hash)(Hash hash)
    if (isAssociativeArray!Hash)
{
    return Walk!Hash(hash);
}

void main()
{
    string[string] hash = ["f" : "foo", "b" : "bar"];

    foreach (i, key, val; hash.walk)
    {
        writefln("%s %s %s", i, key, val);
    }
}
----

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