Re: Array Contained By Array Question

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "Arthur M(dot) Kang" <arthurmkang(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Array Contained By Array Question
Date: 2011-06-10 17:31:07
Message-ID: BANLkTim=R64m-hjhG_1iHmNsQ30zc+iN9Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Jun 10, 2011 at 11:00 AM, Arthur M. Kang <arthurmkang(at)gmail(dot)com> wrote:
> How can I search a multidimensional array for an EXACT "subarray" match?
>
> The following produces true when I want it to produce false.  Obviously, I
> would only want it to produce true for ARRAY[1,2], ARRAY[3,4], or
> ARRAY[5,6].
>
> SELECT ARRAY[ARRAY[1,2], ARRAY[3,4], ARRAY[5,6]] @> ARRAY[ARRAY[1,6]];
>
> Any help is greatly appreciated.

create or replace function slice_in_array(needle anyarray, haystack
anyarray) returns bool as
$$
select $1 in (select $2[v:v] from generate_series(1, array_upper($2, 1)) v);
$$ language sql immutable;

select slice_in_array(array[array[1,2]], ARRAY[ARRAY[1,2], ARRAY[3,4],
ARRAY[5,6]]);

-- or --

create or replace function slice_in_array(needle anyarray, haystack
anyarray) returns bool as
$$
select array[$1] in (select $2[v:v] from generate_series(1,
array_upper($2, 1)) v);
$$ language sql immutable;

select slice_in_array(ARRAY[1,2], ARRAY[ARRAY[1,2], ARRAY[3,4], ARRAY[5,6]]);

(not really sure what the deal is with the operator)

merlin

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Arthur M. Kang 2011-06-10 20:38:10 Re: Array Contained By Array Question
Previous Message Arthur M. Kang 2011-06-10 16:00:23 Array Contained By Array Question