Array reduction operator question



I am expecting the result queue to be populated with the number equal to the one present both in arr and q. The below equation i have doesnt give expected result.

module top();
  int q[$];
  int result[$];
  int arr[$];

  initial begin
    arr = {9,1,8};
    q = {9, 1, 8, 3, 4, 4};
    $display("1. queue 'q' in decimal : %p", q);

    

   
    result =  q.find(i) with (i == arr[i]);
    $display("1. find with i inside arr[i] : %p",result  );
   
  end
endmodule : top

Expected result[$] = 9,1,8;

In reply to rag123:


I am expecting the result queue to be populated with the number equal to the one present both in arr and q. The below equation i have doesnt give expected result.
module top();
int q[$];
int result[$];
int arr[$];
initial begin
arr = {9,1,8};
q = {9, 1, 8, 3, 4, 4};
$display("1. queue 'q' in decimal : %p", q);
result =  q.find(i) with (i == arr[i]);
$display("1. find with i inside arr[i] : %p",result  );
end
endmodule : top
Expected result[$] = 9,1,8;

I think you just need to use

result =  q.find(i) with (i == arr[i.index]});

Regarding your code ask yourself this, what would be the value when i=9, of are[9]?
HTH,
-R

In reply to rag123:

I think i got it working.



result =  q.find(i) with (arr[i] == q[i]); 
    $display("1. find with i inside arr[i] : %p",result  );

but i am not sure why the below one is not working, they both are same?

result =  arr.find(i) with (arr[i] == q[i]); 
 $display("1. find with i inside arr[i] : %p",result  );


In reply to rag123:
i represents each element of the queue/array not the index, you may need to use i.index
Also I’m not sure if I understand your question do you want to find all the elements of q that are present in arr irrespective of their position, or do you want to find if q[0] == arr [0] and so on.

In reply to rag123:

module top();
int q[]; int result[];
int arr[$];

initial begin
arr = {9,1,8};
q = {9, 1, 8, 3, 4, 4};
$display(“1. queue ‘q’ in decimal : %p”, q);

**//result = q.find(i) with (i == arr[i]);
foreach (arr[i]) begin
result.push_back(int’(arr.find_first(x) with (x == q[i])));
end
$display("1. find with i inside arr[i] : %p ",result );
**
end
endmodule : top

In reply to mannat:

You need to clarify a few things. Does it matter which array might have more elements? How are repeated values handled? For example what would you expect the result of this to be?

arr = {9,1,8,4,6,5,5,5};
q = {9, 1, 8, 3, 4, 4, 5};

In reply to dave_59:

Hi Dave,

Does it matter which array might have more elements?
No it doesn’t.

How are repeated values handled?
I want all the values irrespective of unique.
Later i will unique those values from result queue.

In reply to rag123:

module top();
  int q[$];
  int result[$];
  int arr[$];

  initial begin
    arr = {9,1,8,4,6,5,5,5};
    q = {9, 1, 8, 3, 4, 4, 5};
    $display("q     :  %p", q);
    $display("arr   :  %p", arr);

    foreach (arr[i])
      if (arr[i] inside {q})
        result.push_back(arr[i]);
    result = result.unique();
    $display("result : %p ",result );
  end
endmodule : top