Re: obtaining ARRAY position for a given match

From: Scott Bailey <artacus(at)comcast(dot)net>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: obtaining ARRAY position for a given match
Date: 2009-11-19 18:47:02
Message-ID: 4B0592A6.9060806@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sam Mason wrote:
> On Thu, Nov 19, 2009 at 09:46:42AM -0800, Scott Bailey wrote:
>> We had an idx() function in the _int contrib module. I wonder if it
>> would be useful to write this in C now that _int is deprecated?
>
> Is "idx" really the best name for this? there could be multiple
> occurrences of a value in an array (i.e. it's not a set) and hence why I
> used "firstidx" for the function name. If it's replacing an existing
> function, then compatibility is a good reason.

Well I used idx() because there was already a idx(int[], int) function
with the _int contrib module. Obviously "index" is out of the question.
In other languages, it is assumed you are looking for the first index.
Some allow you to specify an offset to begin searching at. And some
provide another function to get the last index of element.

Here is what other languages are using for similar concept.
PHP - array_search()
Python - index()
Ruby - index()/rindex()
Java - binarySearch()
JavaScript/ActionScript - indexOf()
MySQL - find_in_set()

Scott

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Bailey 2009-11-19 18:59:50 Possible bug with array_agg
Previous Message Pavel Stehule 2009-11-19 18:33:07 Re: obtaining ARRAY position for a given match