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 18:00:48
Message-ID: 20071017130048.0ddde862@sinkhole.intrcomm.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

<snip>

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

Works like a champ on 8.1.

Thanks!

Did you see David Fetter's reply to the original post? He has
an interesting alternative.

Josh

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Josh Trutwin 2007-10-17 18:02:07 Re: Array intersection
Previous Message David Fetter 2007-10-17 17:04:21 Re: Array intersection