# Nested associative arrays OR associative arrays of dynamic arrays

**URL:** <https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [May 2, 2016, 4:31pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779 "2016-05-02T16:31:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi.ck.kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@ravi.ck.kumar](https://verificationacademy.com/forums/u/ravi.ck.kumar)\
**Post date:** [May 2, 2016, 4:31pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/1 "2016-05-02T16:31:50Z")

</div>

Hi Folks,

I would like to create an associative array where a string key points to another associative array or to a reference of one.

Similarly I would like to create an associative array where a string key points to a dynamic string array.

Does the language allow the above data types?

-ravi

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 2, 2016, 5:13pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/2 "2016-05-02T17:13:43Z")

</div>

\*In reply to [ravi.ck.kumar](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779):\*Yes. SystemVerilog supports arrays whose elements can be of any type. When dealing with dynamically sized array, you can think of it as arrays of arrays instead of multi-dimensional arrays. because an array element in each dimension can have a different number of elements.  
You can declare arrays of arrays by using an intermediate typedef (preferred)

```verilog
typedef int int_index_by_str_t[string];
int_index_by_str_t lookup[];
or
int lookup[][string];

```

---

<div class="post-metadata">

**Author:** ![ravi.ck.kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@ravi.ck.kumar](https://verificationacademy.com/forums/u/ravi.ck.kumar)\
**Post date:** [May 2, 2016, 5:26pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/3 "2016-05-02T17:26:01Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/2):_

Hi David,  
Thanks for the pointers. Based on that I created a quick example…

module mytest();

typedef string AADD[\*]; // create a hash type  
AADD arrayOfAAs; // create an array of hashes  
arrayOfAAs AAWC2[string]; // create a hash of ‘array-of-hashes’ \<\<— This Doesn’t.  
AADD AAWC3[string]; // create a hash of hash. \<\<---- This Works!!

…

I get the following error. I would like to have a string to reference an array of hashes OR even  
Error-[SE] Syntax error  
Following verilog source has syntax error :  
memory of unknown type  
“a.sv”, 6: token is ‘;’  
arrayOfAAs AAWC2[string]; // create a hash of ‘array-of-hashes’

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 2, 2016, 6:33pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/4 "2016-05-02T18:33:32Z")

</div>

\*In reply to [ravi.ck.kumar](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/3):\*You need to use a type when declaring a variable.

```verilog
typedef AADD arrayOfAAs[]; // create type of an array of hashes
arrayOfAAs AAWC2[string]; // create a hash of 'array-of-hashes'

```

or

```verilog
AADD arrayOfAAs[]; // create an array of hashes
var type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes' 

```

Also, I do not recommend ever using a wildcard associative array index type. Use an actual type. The wildcard index type is antiquated and cannot be used with a number of other systemverilog features.

---

<div class="post-metadata">

**Author:** ![ravi.ck.kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@ravi.ck.kumar](https://verificationacademy.com/forums/u/ravi.ck.kumar)\
**Post date:** [May 2, 2016, 6:42pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/5 "2016-05-02T18:42:16Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/4):_

The second typedef did the trick! Thanks much. Also true about using wildcards. Very little utility in the first place IMHO.

---

<div class="post-metadata">

**Author:** ![yourcheers](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/5f9b8f/32.png) [@yourcheers](https://verificationacademy.com/forums/u/yourcheers)\
**Post date:** [May 2, 2016, 11:43pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/6 "2016-05-02T23:43:34Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/5):_

> \*In reply to [ravi.ck.kumar](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/5):\*You need to use a type when declaring a variable.

```verilog
typedef AADD arrayOfAAs[]; // create type of an array of hashes
arrayOfAAs AAWC2[string]; // create a hash of 'array-of-hashes'

```

or

```verilog
AADD arrayOfAAs[]; // create an array of hashes
var type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes' 

```

Also, I do not recommend ever using a wildcard associative array index type. Use an actual type. The wildcard index type is antiquated and cannot be used with a number of other systemverilog features.

```verilog
AADD arrayOfAAs[]; // create an array of hashes
type(arrayOfAAs) AAWC2[string]; // create a hash of 'array-of-hashes' 

Is this mode of type declaration supported in system-verilog?
```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 2, 2016, 11:53pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/7 "2016-05-02T23:53:47Z")

</div>

_In reply to [yourcheers](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/6):_  
Yes. See 6.23 Type operator of the 1800-2012 LRM. I forgot to add the **var** keyword in front of the type. I corrected my example.

---

<div class="post-metadata">

**Author:** ![shlok13](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@shlok13](https://verificationacademy.com/forums/u/shlok13)\
**Post date:** [November 6, 2019, 7:33am UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/8 "2019-11-06T07:33:17Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/7):_

hi Dave , can we create a typedef using some previous typedef like :  
typedef enum bit [1:0] {OKAY, ERROR, RETRY, SPLIT} response  
so i want to create an associative array using response as input and logic [31:0] as key  
typedef resp\_t address\_driver\_resp [logic [31:0]] ; // will this work ?

if yes , how do we register it in our transaction\_item ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [November 6, 2019, 1:27pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/9 "2019-11-06T13:27:45Z")

</div>

_In reply to [DV@Engineer](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/8):_  
An associative array can have any element type, and any index type as long as equality is defined for the index type.

What do you mean by “register it”?

---

<div class="post-metadata">

**Author:** ![rlanier1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rlanier1](https://verificationacademy.com/forums/u/rlanier1)\
**Post date:** [August 8, 2024, 4:38pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/10 "2024-08-08T16:38:49Z")

</div>

I am unclear by what you mean an array of hashes. Is this the same thing as using a key into an array of arrays?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 8, 2024, 8:08pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/11 "2024-08-08T20:08:49Z")

</div>

People use many different terms for what SystemVerilog calls an _associative array_: map, dictionary, symbol table, or hash. However a _hash_ usually refers to the lookup mechanism used to access an element, not necessarily an array. I tend to avoid it, but was just repeating terminology the previous post used.

---

<div class="post-metadata">

**Author:** ![prashantg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@prashantg](https://verificationacademy.com/forums/u/prashantg)\
**Post date:** [October 4, 2024, 4:37am UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/12 "2024-10-04T04:37:06Z")

</div>

Looks so counter intuitive.

```auto
int lookup[][string]

```

feels more like an associative array of dynamic arrays.

But it is the other way round.

Why is this done?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 4, 2024, 2:55pm UTC](https://verificationacademy.com/forums/t/nested-associative-arrays-or-associative-arrays-of-dynamic-arrays/31779/13 "2024-10-04T14:55:12Z")

</div>

When declaring the 2-dimensional fixed sized array `int A[10][20]`, 10 ius the first dimension and 20 is the second dimension. Selecting `A[0]` gives you a 1-dimensional array of 20 elements.

Similarly selecting `lookup[0]` should give you an associative array.
