Re: LRANGE negative offsets - correct usage?

483 views
Skip to first unread message

Josiah Carlson

unread,
Nov 24, 2012, 1:24:35 PM11/24/12
to redi...@googlegroups.com
Redis does not offer the ability to reverse items as part of the
return of an LRANGE. So you are doing nothing wrong, except trying to
do so.

Assuming that you are accessing Redis from a client library (and not
just redis-cli), you should be able to reverse the return values
programmatically after receiving them from Redis.

Regards,
- Josiah

On Fri, Nov 23, 2012 at 2:31 PM, Ashwin Jayaprakash
<ashwin.ja...@gmail.com> wrote:
> The docs page on LRANGE says negative numbers are allowed, but I tried these
> simple offsets and it doesn't seem to work for iterating over the list from
> right to left:
>
> What am I doing wrong with the list offsets - see marked in red. How do I
> retrieve the elements from -1 to the beginning?
>
>> X:\redis\redis-2.4.11-MSOpenTech>redis-cli.exe
>>
>> redis 127.0.0.1:6379> lpush names "aaa"
>> (integer) 1
>> redis 127.0.0.1:6379> lpush names "bbb"
>> (integer) 2
>> redis 127.0.0.1:6379> lpush names "ccc"
>> (integer) 3
>> redis 127.0.0.1:6379> lpush names "ddd"
>> (integer) 4
>>
>> redis 127.0.0.1:6379> lrange names 0 -1
>> 1) "ddd"
>> 2) "ccc"
>> 3) "bbb"
>> 4) "aaa"
>> redis 127.0.0.1:6379> lrange names -1 0
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names 0 1
>> 1) "ddd"
>> 2) "ccc"
>> redis 127.0.0.1:6379> lrange names 1 4
>> 1) "ccc"
>> 2) "bbb"
>> 3) "aaa"
>> redis 127.0.0.1:6379> lrange names 1 6
>> 1) "ccc"
>> 2) "bbb"
>> 3) "aaa"
>>
>> redis 127.0.0.1:6379> lrange names -1 6
>> 1) "aaa"
>> redis 127.0.0.1:6379> lrange names -1 100
>> 1) "aaa"
>> redis 127.0.0.1:6379> lrange names -1 -4
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names -1 -2
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names -1 0
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names -1 2
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names -1 2
>> (empty list or set)
>> redis 127.0.0.1:6379> lrange names -1 6
>> 1) "aaa"
>> redis 127.0.0.1:6379> lrange names -1 7
>> 1) "aaa"
>> redis 127.0.0.1:6379> lrange names -1 8
>> 1) "aaa"
>>
>> redis 127.0.0.1:6379> llen names
>> (integer) 4
>
>
>
> Thanks,
> Ashwin.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Redis DB" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/redis-db/-/yj8hUL4WzM4J.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to
> redis-db+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/redis-db?hl=en.

Jay A. Kreibich

unread,
Nov 25, 2012, 2:12:19 PM11/25/12
to redi...@googlegroups.com
On Fri, Nov 23, 2012 at 02:31:41PM -0800, Ashwin Jayaprakash scratched on the wall:
> The docs page on LRANGE says negative numbers are allowed, but I tried
> these simple offsets and it doesn't seem to work for iterating over the
> list from right to left:

The docs state that you can't do that:

http://redis.io/commands/lrange

"If start is larger than the end of the list, an empty
list is returned."

What might not be clear is that "larger" refers to the adjusted list
index, not the absolute value passed to LRANGE. Negative indexes are
converted to positive indexes before Redis makes sure the start value
is smaller than the end value.

> What am I doing wrong with the list offsets - see marked in red. How do I
> retrieve the elements from -1 to the beginning?

You don't. You retrieve the elements from the beginning to the end
and reverse them in code.

Or you insert the elements in the opposite order using RPUSH rather
than LPUSH.

> > redis 127.0.0.1:6379> lrange names -1 6
> > 1) "aaa"
> > redis 127.0.0.1:6379> lrange names -1 100
> > 1) "aaa"

Since there are four elements in the list, the -1 start value is
converted to an index of 3.

The docs explain:

"If stop is larger than the actual end of the list, Redis will
treat it like the last element of the list."

So both of these are converted to "LRANGE names 3 3".

> > redis 127.0.0.1:6379> lrange names -1 -4
> > (empty list or set)
> > redis 127.0.0.1:6379> lrange names -1 -2
> > (empty list or set)
> > redis 127.0.0.1:6379> lrange names -1 0
> > (empty list or set)
> > redis 127.0.0.1:6379> lrange names -1 2
> > (empty list or set)
> > redis 127.0.0.1:6379> lrange names -1 2
> > (empty list or set)

Start value (-1 => 3) refers to an element that is larger than the
end value. Empty set returned, as the docs describe.

> > redis 127.0.0.1:6379> lrange names -1 6
> > 1) "aaa"
> > redis 127.0.0.1:6379> lrange names -1 7
> > 1) "aaa"
> > redis 127.0.0.1:6379> lrange names -1 8
> > 1) "aaa"

Adjusted out-of-range end value, just like the first two. All these
are converted to "LRANGE names 3 3".

-j

--
Jay A. Kreibich < J A Y @ K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
but showing it to the wrong people has the tendency to make them
feel uncomfortable." -- Angela Johnson

Ashwin Jayaprakash

unread,
Nov 26, 2012, 12:41:14 PM11/26/12
to redi...@googlegroups.com, j...@kreibi.ch
Thanks.
Reply all
Reply to author
Forward
0 new messages