Jump to page: 1 2
Thread overview
Numerical Index in Associative Foreach
Jul 30, 2005
AJG
Jul 30, 2005
pragma
Jul 30, 2005
Chris Sauls
Jul 31, 2005
AJG
Jul 31, 2005
Chris Sauls
Aug 01, 2005
AJG
Aug 01, 2005
Derek Parnell
Aug 01, 2005
AJG
Aug 01, 2005
Derek Parnell
Aug 01, 2005
AJG
Aug 01, 2005
Derek Parnell
July 30, 2005
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.


July 30, 2005
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
July 30, 2005
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
July 31, 2005
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.



July 31, 2005
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
August 01, 2005
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.


August 01, 2005
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
August 01, 2005
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.


August 01, 2005
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
August 01, 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.


« First   ‹ Prev
1 2