digitalmars.D.learn - Numerical Index in Associative Foreach
- AJG (15/15) Jul 30 2005 Hi there,
- pragma (4/23) Jul 30 2005 AFAIK, there's no direct way to do this via "foreach". The best I can o...
- Chris Sauls (8/22) Jul 30 2005 Not really. But there is:
- AJG (5/10) Jul 30 2005 Is .keys a constant operation? I mean, is this array kept in memory, or ...
- Chris Sauls (5/14) Jul 31 2005 Its a darn good question... and I wasn't sure, so I went poking around i...
- AJG (10/23) Jul 31 2005 Ah. Well, thanks for the info. It appears I'll have to stick to the ugly
- Derek Parnell (9/29) Jul 31 2005 Just out of curiosity, and in no way a criticism, what are planning to d...
- AJG (14/22) Jul 31 2005 It's for a database query generation function. Something along the lines...
- Derek Parnell (24/52) Jul 31 2005 LOL...
- AJG (10/27) Aug 01 2005 I believe you mean "if (result[i].length > 0)", right?
- Derek Parnell (8/11) Aug 01 2005 I use a decent newsreader program - '40tude Dialog'. I think its only th...
Hi there, With a regular array you can do this: And with an associative array you can do this: But what about getting the numerical index in that second example? Is there a nice elegant way to do it? Something like: 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
In article <dcgcpo$1464$1 digitaldaemon.com>, AJG says...Hi there, With a regular array you can do this: And with an associative array you can do this: But what about getting the numerical index in that second example? Is there a nice elegant way to do it? Something like: 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
AJG wrote:Hi there, With a regular array you can do this: And with an associative array you can do this: 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: Not particularly performant. -- Chris Sauls
Jul 30 2005
Hi,Not really. But there is: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
AJG wrote: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 SaulsNot really. But there is:Is .keys a constant operation? I mean, is this array kept in memory, or is it built up just for that property?
Jul 31 2005
Hi,AJG wrote:Ah. Well, thanks for the info. It appears I'll have to stick to the ugly For now. Cheers, --AJG.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.Not really. But there is:Is .keys a constant operation? I mean, is this array kept in memory, or is it built up just for that property?
Jul 31 2005
On Sat, 30 Jul 2005 17:18:48 +0000 (UTC), AJG wrote:Hi there, With a regular array you can do this: And with an associative array you can do this: But what about getting the numerical index in that second example? Is there a nice elegant way to do it? Something like: 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
Hi Derek,(size_t index, char[] key, char c; s);It's for a database query generation function. Something along the lines of: 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.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?
Jul 31 2005
On Mon, 1 Aug 2005 05:51:53 +0000 (UTC), AJG wrote:Hi Derek,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(size_t index, char[] key, char c; s);It's for a database query generation function. Something along the lines of: 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.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?
Jul 31 2005
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
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