# How to retrieve all values at a particular nth index in a multi dimensional associative array?

**URL:** <https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Multi-dimensional-arrays, associative-array, for-loop\
**Created:** [February 22, 2016, 9:20am UTC](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533 "2016-02-22T09:20:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kambhamvivekshankar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kambhamvivekshankar](https://verificationacademy.com/forums/u/kambhamvivekshankar)\
**Post date:** [February 22, 2016, 9:20am UTC](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/1 "2016-02-22T09:20:59Z")

</div>

I have a 3D associative array.

```verilog
array[string][string][string];

```

I want to retrieve all values at a particular 2nd index, say “hello”;

```verilog
array[i]["hello"][k];

```

I tried using,

```verilog
foreach(array[i,,k])
    $display("%d",array[i]["hello"][k]);

```

and also,

```verilog
foreach(array[i])
foreach(array[,,k])
$display("%d",array[i]["hello"][k]);

```

Both didnt work. Any suggestions?? Is it possible??

---

<div class="post-metadata">

**Author:** ![Anudeep\_J](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c68b51/32.png) [@Anudeep\_J](https://verificationacademy.com/forums/u/Anudeep_J)\
**Post date:** [February 23, 2016, 11:59am UTC](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/2 "2016-02-23T11:59:15Z")

</div>

_In reply to [kambhamvivekshankar](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533):_

Hi,

Just try this way, It will work.

```verilog
foreach(array[i,j,k])
    $display("%d",array[i]["hello"][k]);

```

But in foreach just check for existence of element. If the element doesnot exists , it will return default value.

---

<div class="post-metadata">

**Author:** ![kambhamvivekshankar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kambhamvivekshankar](https://verificationacademy.com/forums/u/kambhamvivekshankar)\
**Post date:** [February 23, 2016, 12:34pm UTC](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/3 "2016-02-23T12:34:56Z")

</div>

_In reply to [Anudeep J](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/2):_

Hi Anudeep,

This approach will loop through unnecessary values of j right? Anyway to overcome it.

As you mentioned, some of the elements may not exists. I am looking for a way to constrain values of i and k to provide only valid indices. Any suggestions on how this can be achieved, are welcome.

Thank you

Regards,  
KVS

---

<div class="post-metadata">

**Author:** ![Anudeep\_J](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c68b51/32.png) [@Anudeep\_J](https://verificationacademy.com/forums/u/Anudeep_J)\
**Post date:** [February 23, 2016, 1:05pm UTC](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/4 "2016-02-23T13:05:34Z")

</div>

_In reply to [kambhamvivekshankar](https://verificationacademy.com/forums/t/how-to-retrieve-all-values-at-a-particular-nth-index-in-a-multi-dimensional-associative-array/31533/3):_

Ya, that’s why I mentioned you to have exists check, so that values which are not written or does not exists are not going to be displayed.
