www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - key membership in multi-dimensional assoc. array

reply Paul <phshaffer gmail.com> writes:
I found I can check for key membership in a multi-D aa...
```d
byte zKey = someval;
byte[byte][byte][byte] cubelist;

foreach(byte xK, yzcubelist; cubelist) {
   foreach(byte yK, zcubelist; yzcubelist) {
     foreach(byte zK, val; zcubelist) {
```
with this expression...
```d
     if(zKey in cubelist[xK][yK])
```
Is there a way to check for membership in the x & y dimensions?
```d
     if(yKey in cubelist[xK] ??? [zK])
```
*Thanks in advance for any ideas or solutions.*
Jun 14 2023
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 15 June 2023 at 01:47:45 UTC, Paul wrote:
 I found I can check for key membership in a multi-D aa...
 ```d
 byte zKey = someval;
 byte[byte][byte][byte] cubelist;

 foreach(byte xK, yzcubelist; cubelist) {
   foreach(byte yK, zcubelist; yzcubelist) {
     foreach(byte zK, val; zcubelist) {
 ```
 with this expression...
 ```d
     if(zKey in cubelist[xK][yK])
 ```
 Is there a way to check for membership in the x & y dimensions?
 ```d
     if(yKey in cubelist[xK] ??? [zK])
 ```
 *Thanks in advance for any ideas or solutions.*
`&&`
Jun 14 2023
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/14/23 9:47 PM, Paul wrote:
 I found I can check for key membership in a multi-D aa...
 ```d
 byte zKey = someval;
 byte[byte][byte][byte] cubelist;
 
 foreach(byte xK, yzcubelist; cubelist) {
    foreach(byte yK, zcubelist; yzcubelist) {
      foreach(byte zK, val; zcubelist) {
 ```
 with this expression...
 ```d
      if(zKey in cubelist[xK][yK])
 ```
 Is there a way to check for membership in the x & y dimensions?
 ```d
      if(yKey in cubelist[xK] ??? [zK])
 ```
 *Thanks in advance for any ideas or solutions.*
Not in as short code. You could write a helper though: ```d auto deepIn(V, K, Keys...)(V[K] aa, Keys keys) if (Keys.length > 0) { auto v = keys[0] in aa; static if(keys.length == 1) return v; else return v ? deepIn(*v, keys[1 .. $]) : null; } if(auto v = cubelist.deepIn(xKey, yKey, zKey)) { // v now points at the value } ``` -Steve
Jun 14 2023
parent Paul <phshaffer gmail.com> writes:
On Thursday, 15 June 2023 at 02:21:16 UTC, Steven Schveighoffer 
wrote:
 Not in as short code. You could write a helper though:

 ```d
 auto deepIn(V, K, Keys...)(V[K] aa, Keys keys) if (Keys.length
 0)
{ auto v = keys[0] in aa; static if(keys.length == 1) return v; else return v ? deepIn(*v, keys[1 .. $]) : null; } if(auto v = cubelist.deepIn(xKey, yKey, zKey)) { // v now points at the value } ``` -Steve
Thanks Steve.
Jun 14 2023