# Constraint existence of value into an array

**URL:** <https://verificationacademy.com/forums/t/constraint-existence-of-value-into-an-array/39284>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint, randomization, array-methods, arrays\
**Created:** [July 8, 2021, 3:04am UTC](https://verificationacademy.com/forums/t/constraint-existence-of-value-into-an-array/39284 "2021-07-08T03:04:40Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [July 8, 2021, 7:05am UTC](https://verificationacademy.com/forums/t/constraint-existence-of-value-into-an-array/39284/4 "2021-07-08T07:05:56Z")

</div>

_In reply to [nimrodw](https://verificationacademy.com/forums/t/constraint-existence-of-value-into-an-array/39284/3):_

You cannot have expressions containing unpacked arrays unless you use a foreach loop or a reduction method.

```verilog
class some_object extends uvm_object;
  rand bit [1:0] arr[6];
  constraint the_constraint_c{
    arr.sum() with {int'(item==1)) == 1;
    arr.sum() with {int'(item==2)) == 1;
    arr.sum() with {int'(item==3)) == 1;
  }
 
function new(string name = "some_object");
  super.new(name);
endfunction
endclass: some_object

```

I am constraining the sum of the number of times each elements equals 1,2, or 3 to be 1.

---

_[View the full topic](https://verificationacademy.com/forums/t/constraint-existence-of-value-into-an-array/39284)._
