www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Numerical Index in Associative Foreach

reply AJG <AJG_member pathlink.com> writes:
Hi there,

With a regular array you can do this:

# char[] s;
# foreach (size_t index, char c; s);

And with an associative array you can do this:

# char[char[]] s;
# foreach (char[] key, char c; s);

But what about getting the numerical index in that second example? Is there a
nice elegant way to do it? Something like:

# char[char[]] s;
# foreach (size_t index, char[] key, char c; s);

That doesn't work, because it says it can only take one or two arguments, but
perhaps there's another solution?

Thanks!
--AJG.
Jul 30 2005
next sibling parent pragma <pragma_member pathlink.com> writes:
In article <dcgcpo$1464$1 digitaldaemon.com>, AJG says...
Hi there,

With a regular array you can do this:

# char[] s;
# foreach (size_t index, char c; s);

And with an associative array you can do this:

# char[char[]] s;
# foreach (char[] key, char c; s);

But what about getting the numerical index in that second example? Is there a
nice elegant way to do it? Something like:

# char[char[]] s;
# foreach (size_t index, char[] key, char c; s);

That doesn't work, because it says it can only take one or two arguments, but
perhaps there's another solution?

AFAIK, there's no direct way to do this via "foreach". The best I can offer is a rather brain-dead workaround:
 char[char[]] s;
 uint i=0;
 foreach(char[] key, char c; s){
   /* the rest of your loop */
   i++;
 }

- EricAnderton at yahoo
Jul 30 2005
prev sibling next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
AJG wrote:
 Hi there,
 
 With a regular array you can do this:
 
 # char[] s;
 # foreach (size_t index, char c; s);
 
 And with an associative array you can do this:
 
 # char[char[]] s;
 # foreach (char[] key, char c; s);
 
 But what about getting the numerical index in that second example? Is there a
 nice elegant way to do it?

Not really. But there is: # char[char[]] aa; # foreach (size_t index, char[] key; aa.keys) { # char value = aa[key]; # } Not particularly performant. -- Chris Sauls
Jul 30 2005
parent reply AJG <AJG_member pathlink.com> writes:
Hi,

Not really.  But there is:

# char[char[]] aa;
# foreach (size_t index, char[] key; aa.keys) {
#   char value = aa[key];
# }

Is .keys a constant operation? I mean, is this array kept in memory, or is it built up just for that property? Thanks, --AJG.
Jul 30 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
AJG wrote:
Not really.  But there is:

# char[char[]] aa;
# foreach (size_t index, char[] key; aa.keys) {
#   char value = aa[key];
# }

Is .keys a constant operation? I mean, is this array kept in memory, or is it built up just for that property?

Its a darn good question... and I wasn't sure, so I went poking around in the Phobos 'internal' package. Looking at internal.aaA._aaKeys it appears to me that its built up as needed. Pity. Probably some reason for it, I suppose. -- Chris Sauls
Jul 31 2005
parent AJG <AJG_member pathlink.com> writes:
Hi,

AJG wrote:
Not really.  But there is:

# char[char[]] aa;
# foreach (size_t index, char[] key; aa.keys) {
#   char value = aa[key];
# }

Is .keys a constant operation? I mean, is this array kept in memory, or is it built up just for that property?

Its a darn good question... and I wasn't sure, so I went poking around in the Phobos 'internal' package. Looking at internal.aaA._aaKeys it appears to me that its built up as needed. Pity. Probably some reason for it, I suppose.

Ah. Well, thanks for the info. It appears I'll have to stick to the ugly # size_t i = 0; # foreach(...) { # ... # ++i; # } For now. Cheers, --AJG.
Jul 31 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 30 Jul 2005 17:18:48 +0000 (UTC), AJG wrote:

 Hi there,
 
 With a regular array you can do this:
 
 # char[] s;
 # foreach (size_t index, char c; s);
 
 And with an associative array you can do this:
 
 # char[char[]] s;
 # foreach (char[] key, char c; s);
 
 But what about getting the numerical index in that second example? Is there a
 nice elegant way to do it? Something like:
 
 # char[char[]] s;
 # foreach (size_t index, char[] key, char c; s);
 
 That doesn't work, because it says it can only take one or two arguments, but
 perhaps there's another solution?

Just out of curiosity, and in no way a criticism, what are planning to do with such an index? As the items are placed into the AA in basically random (hashed) locations, the index for a given item could change with each insert and delete? -- Derek Melbourne, Australia 1/08/2005 3:11:06 PM
Jul 31 2005
parent reply AJG <AJG_member pathlink.com> writes:
Hi Derek,

(size_t index, char[] key, char c; s);
 
 That doesn't work, because it says it can only take one or two arguments, but
 perhaps there's another solution?

Just out of curiosity, and in no way a criticism, what are planning to do with such an index? As the items are placed into the AA in basically random (hashed) locations, the index for a given item could change with each insert and delete?

It's for a database query generation function. Something along the lines of: # string[] generate(string[string][] lists) { # string result[] = new string[lists.length]; # # foreach(size_t i, string[string] list; lists) # foreach(size_t j, string key, string value; list) # result[i] ~= (j ? "," : null) ~ key ~ "=" ~ value; # # return (result); # } Cause you can't have those trailin' commas, can you now? --AJG. PS: Don't quote me on that code. I just wrote it out on the spot.
Jul 31 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 1 Aug 2005 05:51:53 +0000 (UTC), AJG wrote:

 Hi Derek,
 
(size_t index, char[] key, char c; s);
 
 That doesn't work, because it says it can only take one or two arguments, but
 perhaps there's another solution?

Just out of curiosity, and in no way a criticism, what are planning to do with such an index? As the items are placed into the AA in basically random (hashed) locations, the index for a given item could change with each insert and delete?

It's for a database query generation function. Something along the lines of: # string[] generate(string[string][] lists) { # string result[] = new string[lists.length]; # # foreach(size_t i, string[string] list; lists) # foreach(size_t j, string key, string value; list) # result[i] ~= (j ? "," : null) ~ key ~ "=" ~ value; # # return (result); # } Cause you can't have those trailin' commas, can you now? --AJG. PS: Don't quote me on that code. I just wrote it out on the spot.

LOL... Anyhow, if that's all you need then this might suffice ... string[] generate(string[string][] lists) { string[] result; size_t i; result.length = lists.length; foreach(string[string] list; lists) { foreach(string key, string value; list) result[i] ~= key ~ "=" ~ value ~ ","; // drop final comma if (result.length > 0) result[i].length = result[i].length-1; i++; } return (result); } and might be faster too. -- Derek Melbourne, Australia 1/08/2005 4:31:44 PM
Jul 31 2005
parent reply AJG <AJG_member pathlink.com> writes:
Hi,

Anyhow, if that's all you need then this might suffice ...

string[] generate(string[string][] lists) 
{
    string[] result;
    size_t i;

    result.length = lists.length;
    foreach(string[string] list; lists)
    {
        foreach(string key, string value; list)
            result[i] ~= key ~ "=" ~ value ~ ",";
        // drop final comma
        if (result.length > 0)
            result[i].length = result[i].length-1;
        i++;
    }
    return (result);
}

I believe you mean "if (result[i].length > 0)", right? Yeah, I could do that, but mine looks more elegant :p. I like my code as short as possible. What I settled on in my function(s) is to slice the comma out in the end, but I'm still not sure if reducing the length is faster or not. Cheers, --AJG. PS: Oh, yeah, how'd you get the code to indent properly without prefixing the lines with a non-whitespace character? And without [code] tags around it? Thanks.
Aug 01 2005
parent Derek Parnell <derek psych.ward> writes:
On Mon, 1 Aug 2005 15:26:00 +0000 (UTC), AJG wrote:


[snip]

 PS: Oh, yeah, how'd you get the code to indent properly without prefixing the
 lines with a non-whitespace character? And without [code] tags around it?
 Thanks.

I use a decent newsreader program - '40tude Dialog'. I think its only the web interface that messes up indentation. -- Derek Parnell Melbourne, Australia 2/08/2005 7:12:54 AM
Aug 01 2005