Re: Pl/PgSql: Loop over Array

From: Joe Conway <mail(at)joeconway(dot)com>
To: axel(at)straschil(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Pl/PgSql: Loop over Array
Date: 2003-05-23 04:39:56
Message-ID: 3ECDA61C.5050008@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Axel Straschil wrote:
> Ist it possible to loop over an array:
>

If you are using Postgres 7.3.x the following works. If you are using
7.2.x or earlier, you may be able to modify the "high" and "low"
calculation lines using the substr and strpos functions.

CREATE OR REPLACE FUNCTION atest(INT[]) RETURNS TEXT AS '
DECLARE
mya alias for $1;
myret text = '''';
low int;
high int;
BEGIN
low := replace(split_part(array_dims(mya),'':'',1),''['','''')::int;
high := replace(split_part(array_dims(mya),'':'',2),'']'','''')::int;

FOR i IN low..high LOOP
myret := myret || ''|'' || mya[i];
END LOOP;

RETURN myret;
END;
' LANGUAGE 'plpgsql' IMMUTABLE STRICT;

regression=# select atest('{1,2,3,4}'::int[]);
atest
----------
|1|2|3|4
(1 row)

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Kings-Lynne 2003-05-23 05:55:38 phpPgAdmin 3.0 Beta 1 Released
Previous Message Jim C. Nasby 2003-05-23 04:25:43 Re: tablemeta-data