Hi,
I was trying to learn some array methods when I encountered a strange phenomena when trying calculate an array’s sum based on a iterator condition using a “with” clause
To give context, here’s the code
module tb_array_methods;
function void disp_msg(input string tag, input string msg);
$display($sformatf("[%0t][%0s]: %0s", $time, tag, msg));
endfunction
int arr[$] = {21,33,42,20,20,42,11,24,33};
longint res;
int arr_loc[$];
int res_2;
bit b_array[] = '{1,0,0,1,0,1};
// ====================================== //
// Array Locator Methods
// All array locator methods return the results as a queue of type "int" and not "integer"
initial begin
// How about we combine some boolean functions with locators?
//int arr[$] = {21,33,42,20,20,42,11,24,33};
res_2 = arr.sum() with (item>34);
// Returns sum({0,0,1,0,0,1,0,0,0}) = 2;
// This can be used to find the count of certain values
disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
res_2 = arr.sum() with (item * (item>34));
// Returns sum({0,0,42,0,0,42,0,0,0}) = 84;
res_2 = arr.sum() with (item > 34? item:0);
// Returns 84
disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
end
endmodule
When I ran this code, I got this result

But I was expecting a sum of 2. Instead I got a 565641244.
Could anyone please tell me what’s wrong here?
Also when i use
res_2 = arr.sum(x) with (x>34);
I get
res_2 = 111067408
PS: I also noticed that Xilinx says compile issues when i use queues with product() and and(). Is that a xilinx issue? Because in chris spear, he uses them with queues
