Re: How to count elements in an array?

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Grant <grant(at)conprojan(dot)com(dot)au>, sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to count elements in an array?
Date: 2001-03-06 04:52:33
Message-ID: 3AA46D11.885E1483@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Grant,

The only way I know to enumarate arrays is procedural. This is a good
reason to stay away from arrays except in buffer and temporary tables
(aside from the fact that array columns violate the relational principle
of atomicity).

The following sample procedure, paraphrased from Bruce Momjian's book
(which you should buy, hint, hint) illustrates enumerating an array of
INT4 values:

CREATE FUNCTION count_array ( INT4[] ) RETURNS INT2
AS '
DECLARE
array_loop INT2;
array_col ALIAS FOR $1;
BEGIN
array_loop := 1;
WHILE array_col[array_loop] LOOP
array_loop = array_loop + 1;
END LOOP;
array_loop := array_loop - 1
RETURN array_loop;
END;'
LANGUAGE 'plpgsql';

-Josh
--
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Grant 2001-03-06 05:13:21 Re: No Documentation for to_char(INTERVAL, mask)
Previous Message Gregory Wood 2001-03-06 04:48:07 Re: MySQLs Describe emulator!