# SV Part-2 Common Array Methods

1. ### System Verilog Array Locator Methods
    

In System Verilog (SV), array locator methods are built-in functions used to search and filter arrays **(including dynamic arrays, queues, and associative arrays)**. They operate by iterating over the array elements and applying a condition specified in a `with` clause. These methods are divided into two main categories:

* **Element Locator Methods**: Return the elements (as a queue) that match the condition.
    
* **Index Locator Methods**: Return the indices (as a queue) of elements that match the condition.
    

The `with` clause is optional for some methods (e.g., `min`, `max`) but required for others (e.g., `find`). If no iterator is specified in the `with` clause, `item` is used by default to refer to the current element.

These methods work on unpacked arrays and always return a queue (`$`). If no elements match, an empty queue is returned.

Below is a table summarizing the key locator methods, followed by simple examples. All examples use a basic dynamic array for demonstration.

| Method Name | Category | Description | Return Type | `with` Clause Required? |
| --- | --- | --- | --- | --- |
| [`find()`](https://edaplayground.com/x/qbij) | Element | Returns all elements matching the condition. | queue | Yes |
| [`find_first(condition)`](https://edaplayground.com/x/wpgB) | Element | Returns the first element matching the condition. | queue | Yes |
| [`find_last(condition)`](https://edaplayground.com/x/fjxp) | Element | Returns the last element matching the condition. | queue | Yes |
| `find_first_index(condition)` | Index | Returns the index of the first element matching the condition. | queue | Yes |
| [`find_last_index(condition)`](https://edaplayground.com/x/ENMf) | Index | Returns the index of the last element matching the condition. | queue | Yes |
| [`find_index(condition)`](https://edaplayground.com/x/aTGv) | Index | Returns all indices of elements matching the condition. | queue | Yes |
| [`min()`](https://edaplayground.com/x/aTH6) | Element | Returns the element(s) with the minimum value. | queue | No (optional) |
| [`max()`](https://edaplayground.com/x/sbby) | Element | Returns the element(s) with the maximum value. | queue | No (optional) |
| [`unique()`](https://edaplayground.com/x/KHSa) | Element | Returns unique elements (duplicates removed). | queue | No (optional) |
| [`unique_index()`](https://edaplayground.com/x/9cW5) | Index | Returns indices of unique elements. | queue | No (optional) |

### *practice makes men/women perfect*

> try your self if your facing issue [**clikme**](https://edaplayground.com/x/FhQw)

```plaintext
Question
Understanding Queue Operations in a Random Number Generator
A program generates a queue of 20 unique random integers between 100 and 200 (inclusive). It then performs the following operations:

Finds all values in the queue that are greater than 150 and stores them in a new queue.
Finds the indices of all values in the queue that are greater than 150.
Finds all values in the queue that are greater than 120 but less than 170, and stores them in a new queue.
Finds the indices of all values in the queue that are greater than 120 but less than 170.
```

```plaintext
Try same for
  1.Dynamic arry
```

> Dynamic array, try if your self if your facing issue [**clikme**](https://edaplayground.com/x/syGL)

2. ### Ordering Methods
    

**Fixed-size arrays**, **dynamic arrays**, and **queues** support ordering methods in SystemVerilog (SV). These methods such as `reverse()`, `sort()`, `rsort()`, and `shuffle()` reorder elements in single-dimensional unpacked arrays of these types.

Associative arrays do not support these methods, as their elements are inherently unordered beyond the key-based lookup.

| Array Type | Supports Ordering Methods? | Notes |
| --- | --- | --- |
| Fixed-size | Yes | Applies to unpacked fixed arrays. |
| Dynamic | Yes | Applies to unpacked dynamic arrays. |
| Queue | Yes | Queues behave like dynamic arrays with FIFO semantics. |
| Associative | No | Elements are accessed via keys; use iteration for custom ordering. |

| Method | Description | Applicable Array Types | Notes |
| --- | --- | --- | --- |
| [`reverse()`](https://edaplayground.com/x/GJGk) | Reverses the order of elements in the array. | Fixed-size, Dynamic, Queue | Modifies the array in place; no return value. |
| [`sort()`](https://edaplayground.com/x/aTc3) | Sorts the array in ascending order. | Fixed-size, Dynamic, Queue | Modifies the array in place; no return value. |
| [`rsort()`](https://edaplayground.com/x/ZdGY) | Sorts the array in descending order. | Fixed-size, Dynamic, Queue | Modifies the array in place; no return value. |
| [`shuffle()`](https://edaplayground.com/x/D7ft) | Randomly reorders the elements of the array. | Fixed-size, Dynamic, Queue | Modifies the array in place; no return value; uses simulator's random seed. |

3. ### Array Reduction methods
    
    SystemVerilog supports array reduction methods powerful built-in methods that let you reduce an entire array (or queue, dynamic array, associative array) to a single value using a specified operation.
    
    1. [sum](https://edaplayground.com/x/ain4) : it is used to perform the summation of all the array elements.
        
    2. [product](https://edaplayground.com/x/EDCP) : it is used to perform the product of all the array elements.
        
    3. [or](https://edaplayground.com/x/nbvY) : it is used to perform the or operation of the array elements.
        
    4. [and](https://edaplayground.com/x/uYUn) : it is used to perform the and operation of the array elements.
        
    5. [xor](https://edaplayground.com/x/beDC) : it is used to perform the xor operation of the array elements.
        
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762454205494/0bbaa6ee-e4eb-4c95-8b50-603c9f8499a7.jpeg align="center")
