# Is it possible to use find\_index() functions to match nested queues?

**URL:** <https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, queues-systemverilog, classofqueues\
**Created:** [February 24, 2023, 5:42pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315 "2023-02-24T17:42:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![markiv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@markiv](https://verificationacademy.com/forums/u/markiv)\
**Post date:** [February 24, 2023, 5:42pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/1 "2023-02-24T17:42:04Z")

</div>

In a queue of a class/Transaction holding multiple variables and queues.  
Is it possible to use find\_index to search the second level of queues along with the first level?  
Haven’t been able to find any language in the LRM that points to it, alternative is to sequentially match one step at a time.

Adding a simple example below:

```verilog
class A;
  int sub_q1[$];
  int sub_q2[$]
endclass: A

```

```verilog
class B;
A bad_idea_q[$];
  
  function match(int addr1, int addr2);
    int match_q[$];
    match_q = bad_idea_q.find_index with ( item.sub_q1[x]==addr1 && item.sub_q2[y]==addr2);
  endfunction
endclass: B

```

---

<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:** [February 24, 2023, 7:36pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/2 "2023-02-24T19:36:02Z")

</div>

_In reply to [markiv](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315):_

It would help to show some examples with data, and what are you expecting the function. match to return.

---

<div class="post-metadata">

**Author:** ![markiv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@markiv](https://verificationacademy.com/forums/u/markiv)\
**Post date:** [February 24, 2023, 9:23pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/3 "2023-02-24T21:23:02Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/2):_

Function match should return a queue of matching indexes of the bad\_idea\_q which holds sub\_q1==addr1 and sub\_q2[y]==addr2.

```verilog
int match_q[$];
bit stage1, stage2;
foreach(bad_idea_q[i]) begin
 stage1 = 0;
 stage2 = 0;
 foreach(bad_idea_q[i].sub_q1[j]) begin
  if(bad_idea_q[i].sub_q1[j] == addr1) stage1=1;
 end
 foreach(bad_idea_q[i].sub_q2[k]) begin
  if(bad_idea_q[i].sub_q2[k] == addr2) stage2=1;
 end 
 if(stage1&&stage2) match_q.push_back(i);
end

```

–  
Here’s an example  
There are 4 items in the bad\_idea\_q: {1,2,3,4}  
Every item has two sub queues, sub\_q1 and sub\_q2  
item → {sub\_q1} {sub\_q2}  
1 → {a,b,c,d} {e,f,g,h}  
2 → {e,f,g,h} {i,j,k,l}  
3 → {a,b,c,d} {e,f,g,k}  
4 → {e,f,g,h} {l,m,n,o}

Output of match(a,g) → {1,3}  
Output of match(a,k) → {3}  
Output of match(e,l) → {2,4}  
Output of match(e,j) → {2}

–  
Its straightforward to match in a single level search. Curious if there’s a way to simplify searching through the sub queues without a dedicated function. I hope that helped clarify my earlier question

---

<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:** [February 24, 2023, 11:10pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/4 "2023-02-24T23:10:33Z")

</div>

_In reply to [markiv](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/3):_

I think this does what you want:

```verilog
match_q = bad_idea_q.find_index with ( addr1 inside {item.sub_q1} && addr2 inside {item.sub_q2});

```

---

<div class="post-metadata">

**Author:** ![Shubhabrata](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/shubhabrata/32/1644_2.png) [@Shubhabrata](https://verificationacademy.com/forums/u/Shubhabrata)\
**Post date:** [February 26, 2023, 1:31pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/5 "2023-02-26T13:31:35Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/4):_

Hi Sir,  
This code could have also been written in the following way, I guess :

```verilog

match_q = bad_idea_q.find_index with ( addr1 inside {subq1[item]} && addr2 inside {subq2[item]});

```

subq1 and subq2 - both of them are 2D queues. bad\_idea\_q is the queue with the positions/row numbers of those 2D queues.  
Now, when I am writing item.subq1 along with the “with” clause, it will go to that row number and search whether addr1 is present in that row. I believe subq1[item] will do the exact thing.

---

<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:** [February 26, 2023, 4:47pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/6 "2023-02-26T16:47:08Z")

</div>

_In reply to [Shubhabrata](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/5):_

No it will not. Try compiling it.

---

<div class="post-metadata">

**Author:** ![Shubhabrata](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/shubhabrata/32/1644_2.png) [@Shubhabrata](https://verificationacademy.com/forums/u/Shubhabrata)\
**Post date:** [February 27, 2023, 6:32am UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/7 "2023-02-27T06:32:16Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/6):_

Yes, It didn’t work. Is it any characteristic of the “with” clause or limitation of eda tool?

---

<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:** [February 27, 2023, 7:12am UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/8 "2023-02-27T07:12:53Z")

</div>

_In reply to [Shubhabrata](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/7):_

bad\_idea\_q is a queue of class variables, each element containing a handle a class A object.

The with expression iterates replacing ‘item’ with a handle to a class A object.

sub\_q1 and sub\_q2 are members of a class A object. You cannot access then without a reference to a class handle. The error message you get when compiling should point out the problem with the code.

---

<div class="post-metadata">

**Author:** ![markiv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@markiv](https://verificationacademy.com/forums/u/markiv)\
**Post date:** [February 27, 2023, 10:27pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/9 "2023-02-27T22:27:44Z")

</div>

This works, thanks Dave.  
Additionally, is it possible to bit manipulate the variables inside the queue ?  
If I want to shift out the last nibble in both matching variables  
I understand the below code is an illegal operation, is there an alternative?

```verilog
match_q = bad_idea_q.find_index with ( (addr1 >> 4) inside { item.sub_q1 >> 4 } && addr2 inside {item.sub_q2});

```

---

<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:** [February 28, 2023, 6:24am UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/10 "2023-02-28T06:24:08Z")

</div>

_In reply to [markiv](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/9):_  
There is, but it is quite convoluted.

```verilog
match_q = bad_idea_q.find_index with ( item.sub_q1.or(q1) with (addr1 >> 4 == q1 >> 4) && 
                                       item.sub_q2.or(q2) with (addr2 >> 4 == q2 >> 4) );

```

It might be more suitable to write a function.

---

<div class="post-metadata">

**Author:** ![markiv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@markiv](https://verificationacademy.com/forums/u/markiv)\
**Post date:** [February 28, 2023, 5:34pm UTC](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/11 "2023-02-28T17:34:14Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/is-it-possible-to-use-find-index-functions-to-match-nested-queues/41315/10):_

That is quite some sorcery!  
Thank you Dave
