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.
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".
Start value (-1 => 3) refers to an element that is larger than the
end value. Empty set returned, as the docs describe.
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