# Associative arrays

**URL:** <https://verificationacademy.com/forums/t/associative-arrays/34567>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, associative-array\
**Created:** [June 13, 2018, 1:35pm UTC](https://verificationacademy.com/forums/t/associative-arrays/34567 "2018-06-13T13:35:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![verif\_learner1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@verif\_learner1](https://verificationacademy.com/forums/u/verif_learner1)\
**Post date:** [June 13, 2018, 1:35pm UTC](https://verificationacademy.com/forums/t/associative-arrays/34567/1 "2018-06-13T13:35:44Z")

</div>

I just want to make sure I have got this correct as I am going to implement something based on this.  
When I overwrite an entry in an associative array, I see that the new value is rejected, which is what I am looking for.

The following is the code I have tried:

```verilog
module one;
  int Q[int] ;
  int x, y;

  initial begin
    Q[1] = 1;
    Q[2] = 2;
    Q[3] = 3;
    Q[3] = 4;
    $display ("%d %d %d", Q[0], Q[1], Q[2]);
  end
endmodule

```

The following is the output:

```verilog
         0 1 2

```

At least I am not able to find anything in LRM that describes this behaviour

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [June 13, 2018, 2:36pm UTC](https://verificationacademy.com/forums/t/associative-arrays/34567/2 "2018-06-13T14:36:42Z")

</div>

_In reply to [verif\_learner](https://verificationacademy.com/forums/t/associative-arrays/34567):_

What happens when you print Q[3], which is the value you are changing?

---

<div class="post-metadata">

**Author:** ![verif\_learner1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@verif\_learner1](https://verificationacademy.com/forums/u/verif_learner1)\
**Post date:** [June 13, 2018, 3:52pm UTC](https://verificationacademy.com/forums/t/associative-arrays/34567/3 "2018-06-13T15:52:11Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/associative-arrays/34567/2):_

> _In reply to [verif\_learner](https://verificationacademy.com/forums/t/associative-arrays/34567/2):_  
> What happens when you print Q[3], which is the value you are changing?

Sorry about that. I used Q[0] which is actually no existent (until I used it in display statement) and assumed I have displayed all 3 entries.

I checked it again. The latest entry takes effect.  
So, I have to check using exists() function to make sure that I don’t overwrite it if it is already present (which is what I need actually)
