pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Grant <grant@conprojan.com.au>
To: sqllist <pgsql-sql@postgresql.org>
Subject: Re: How to count elements in an array?
Date: Mon, 05 Mar 2001 20:52:33 -0800
Message-ID: <3AA46D11.885E1483@agliodbs.com> (raw)
References: <Pine.LNX.4.21.0103060849160.2044-100000@webster.conprojan.com.au>

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@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco



view thread (2+ messages)

Message-ID: <3AA46D11.885E1483@agliodbs.com>
Permalink:  ../3AA46D11.885E1483@agliodbs.com/
Also on:    postgresql.org/message-id/3AA46D11.885E1483@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, grant@conprojan.com.au
  Subject: Re: How to count elements in an array?
  In-Reply-To: <3AA46D11.885E1483@agliodbs.com>
  References: <Pine.LNX.4.21.0103060849160.2044-100000@webster.conprojan.com.au> <3AA46D11.885E1483@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox