Re: Support for negative index values in array fetching
Hannu Valtonen <hannu.valtonen@f-secure.com>
From: Hannu Valtonen <hannu.valtonen@f-secure.com>
To: Florian Pflug <fgp@phlo.org>
Cc: Pavel Stehule <pavel.stehule@gmail.com>, Peter Eisentraut <peter_e@gmx.net>, pgsql-hackers@postgresql.org
Date: 2011-01-09T14:33:56Z
Lists: pgsql-hackers
On 1/5/11 6:19 PM, Florian Pflug wrote: >> Sorry, but It isn't too intuitive. Minimally for me. Why you don't >> thinking about simple functions with only positive arguments. There >> are only four combinations. I don't think we must have only one super >> function. >> >> we need functionality for: >> >> a) get first n items >> b) get items without last n items >> c) get last n items >> d) skip first n items > Now you've moved the goalpost - the OP wanted to access individual > elements, not slices! To support slices, a three-argument version > of array_relative() would be required, with the signature > > array_relative(some_array anyarray, first int[], last int[]) > > Your requirements (a) to (d) are then easily satisfied > > a) array_relative(ary, array[0], array[n-1]) > b) array_relative(ary, array[0], array[-n-1]) > c) array_relative(ary, array[-n], array[-1]) > d) array_relative(ary, array[n], array[-1]) > > The individual function approach might be a tad more readable for > one-dimensional arrays, but they don't scale well to the general > case. > > Maybe the OP could comment on whether any of these solutions > would fit his needs? > Hi, (sorry for the late reply, got lost in my Inbox) For my main use case I just needed the last element of the array, but I could see myself needing a slice as well. (i.e. give me the last 5 items in an array) So in that sense yes, this would fit the bill. Hannu Valtonen Lead Software Architect Technology Office F-Secure Corporationhttp://www.F-Secure.com