LPOS

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Get the index of a value

The usage is lpos key value.
Indexes start at 0 from the head (left).

Example

Command>rpush mylist A B C 1 2 3 C C
Result>8
Command>lpos mylist A
Result>(integer) 0
Command>lpos mylist C
Result>(integer) 2   The index of the first C is returned.

RANK option: specify which match to find

To find the second C, use RANK 2.
A negative rank searches from the tail (right).

Example

Command>lpos mylist C RANK 2
Result>(integer) 6
Command>lpos mylist C RANK -1
Result>(integer) 7

COUNT option: specify how many matches to return

When the same value appears more than once, specify how many matches to return. Specify 0 to find all of them.

Example

Command>lpos mylist C COUNT 2
Result>1) (integer) 2
2) (integer) 6
Command>lpos mylist C COUNT 0
Result>1) (integer) 2
2) (integer) 6
3) (integer) 7

MAXLEN option: limit how many elements to scan

If the list has a very large number of elements, finding a value can take time. Use this to limit how many elements are scanned so the query does not take too long.
You can use the RANK, COUNT, and MAXLEN options together.

Example

Command>lpos mylist C COUNT 0 MAXLEN 3
Result>1) (integer) 2
Command>lpos mylist C COUNT 0 MAXLEN 7
Result>1) (integer) 2
2) (integer) 6

Notes

If the value is not found, nil is returned.
If the COUNT option is used, (empty array) is returned.

Example

Command>lpos mylist D
Result>(nil)
Command>lpos mylist D COUNT 0
Result>(empty array)


Syntax

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]

  • Available since version 6.0.6.
  • Time complexity is O(N). N is the number of values or the count specified by MAXLEN.

<< LINDEX LPOS LSET >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.