Re: Array intersection

From: Josh Trutwin <josh(at)trutwins(dot)homeip(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Array intersection
Date: 2007-10-17 16:31:51
Message-ID: 20071017113151.3c2df8cf@sinkhole.intrcomm.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 17 Oct 2007 17:08:06 +0100
Sam Mason <sam(at)samason(dot)me(dot)uk> wrote:

> CREATE OR REPLACE FUNCTION array_intersect (array1
> INTEGER[],array2 INTEGER[]) RETURNS INTEGER[] AS $$
> DECLARE
> out INTEGER[];
> BEGIN
> IF array1 IS NULL OR array2 IS NULL THEN
> RETURN '[]';
> END IF;
> FOR i IN ARRAY_LOWER(array1,1) .. ARRAY_UPPER(array1,1) LOOP
> IF (array1[i] =ANY (array2)) THEN
> out := array_append(out,array1[i]);
> END IF;
> END LOOP;
> RETURN out;
> END;
> $$ LANGUAGE PLPGSQL;

Is the =ANY specific to PG 8.2 or higher? On 8.1.10:

psql=> select array_intersect('{1,2,3}', '{1,2,6,7,8}');
array_intersect
-----------------

(1 row)

Also, I think the first return needs to be:

RETURN '{}';

instead of:

RETURN '[]';

Josh

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2007-10-17 16:33:13 Re: Array intersection
Previous Message Josh Trutwin 2007-10-17 16:28:31 Re: Array intersection