# Array locator method (Min and with clause)

**URL:** <https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664>\
**Category:** SystemVerilog\
**Created:** [February 18, 2025, 4:29pm UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664 "2025-02-18T16:29:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![agr\_nimish](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@agr\_nimish](https://verificationacademy.com/forums/u/agr_nimish)\
**Post date:** [February 18, 2025, 4:29pm UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664/1 "2025-02-18T16:29:51Z")

</div>

Hi

I want to find the min value of the array from first 4 locations only. There is one way to take first 4 elements in a different queue/array then use min function, but if I am trying to do in following manner and I am observing an unexpected result.

Can anyone please explain what is the issue?

```auto
module tb;
  int arr[5] = '{4,3,2,1,5};
  int min_val[$];
  
  initial begin
    min_val = arr.min with (item.index < 4);
    $display("min_val %0p",min_val);
  end
  
endmodule

```

Output : min\_val '{5}

---

<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 18, 2025, 6:03pm UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664/2 "2025-02-18T18:03:58Z")

</div>

The `with` clause generates the values used to judge which element has the minimum value, not the values to selected. In your example, it creates the array '{0,1,1,1,0}. 0 is the minimum value, and there are two elements associated with that. So either 4 or 5 could have been selected as the minimum value.

---

<div class="post-metadata">

**Author:** ![agr\_nimish](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@agr\_nimish](https://verificationacademy.com/forums/u/agr_nimish)\
**Post date:** [February 18, 2025, 9:21pm UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664/3 "2025-02-18T21:21:58Z")

</div>

Hi Dave, Thanks for the reply.

If that is how it works then it will generate '{1,1,1,1,0} and always select 0th location element which is 5 in this case. Is my understanding correct?

`(item.index > 4)` : Here why did with clause is generating ‘{0,1,1,1,0} ?

But, Why this works as expected with find method?

```auto
selected_vals = arr.find with (item.index < 4);

```

Thanks

---

<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 18, 2025, 10:47pm UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664/4 "2025-02-18T22:47:53Z")

</div>

Yes I did make a mistake. Only the last element `arr[4]` has `item.index < 4` which is 0. That makes it the sole minimum value.

The `find` method is a filter-- it selects elements when the `with` clause is true. Here’s another example that might make it more clear how these method behave.

```
int arr[] = {9,8,7,6,5,4,3,2,1};
max_val = arr.max() with ($countones(item));

```

That returns {7}, because $countones(7) is 3, and all the other elements are 1 or 2.

```
max_val = arr.max() with ($countones(item.index));

```

That returns {2} because arr[7] has the value 2.

---

<div class="post-metadata">

**Author:** ![agr\_nimish](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@agr\_nimish](https://verificationacademy.com/forums/u/agr_nimish)\
**Post date:** [February 19, 2025, 6:21am UTC](https://verificationacademy.com/forums/t/array-locator-method-min-and-with-clause/49664/5 "2025-02-19T06:21:46Z")

</div>

Thanks Dave. I got it now.
