0

I created a list of structs and I like to sum the value of a field in each struct that satisfied a specific condition. For example:

struct packet {
   val:int;
   cond:bool;
};
l:list of packet;

When I write the following:

keep l.all(it.cond).sum(it.val) == 1000;
I get an error: GEN_NO_GENERATABLE_NOTIF.

When I define a result variable:

sum_val : int;
keep sum_val == 100;

and change the constraint to: keep l.all(it.cond).sum(it.val) == sum_val;

I get a contradiction!

How do I make it work?

Semadar
  • 105
  • 6

2 Answers2

3

You should use the list pseudo method "sum" and the ternary operation:

keep l.sum(it.cond ? It.val : 0) == 1000
Amit M.
  • 208
  • 1
  • 3
Assaf
  • 79
  • 4
0

From the question it seems you want to sum some elements in existing list, not generate a new one. In this case, use simply : var sum : int = l.all(it.cond).sum(it.val);