X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AEF5A329D7F for ; Tue, 21 Sep 2004 05:44:05 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41222-06 for ; Tue, 21 Sep 2004 04:44:03 +0000 (GMT) Received: from joeconway.com (wsip-24-249-201-67.sd.sd.cox.net [24.249.201.67]) by svr1.postgresql.org (Postfix) with ESMTP id 2FDE6329D41 for ; Tue, 21 Sep 2004 05:44:03 +0100 (BST) Received: from [192.168.4.3] (account jconway [192.168.4.3] verified) by joeconway.com (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 2198380; Mon, 20 Sep 2004 21:35:25 -0700 Message-ID: <414FB192.5060100@joeconway.com> Date: Mon, 20 Sep 2004 21:44:02 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803 X-Accept-Language: en-us, en MIME-Version: 1.0 To: sergio.fantinel@lnl.infn.it Cc: pgsql-sql@postgresql.org Subject: Re: PL/pgSQL multidimension (matrix) array in function References: <4149C90D.60806@lnl.infn.it> In-Reply-To: <4149C90D.60806@lnl.infn.it> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/181 X-Sequence-Number: 19027 Sergio Fantinel wrote: > I found how to use, inside a PL/pgSQL function, a two-dimensions array > (matrix). > There is a limitation: the number of the 'columns' of the matrix is > fixed at declaration time (in DECLARE section) and you need to manually > initialize all the elements in the first 'row' of the matrix. You should use '{}' to initialize the array to empty. See below for an example: CREATE OR REPLACE FUNCTION testarray (integer, integer) RETURNS SETOF integer[] AS' DECLARE n alias for $1; -- number of rows is passed as argument i INTEGER; j integer; k alias for $2; -- matrix columns number a integer[]; begin for i in 1..n loop a := ''{}''; -- create empty array for j in 1..k loop a := a || i; return next a; end loop; end loop; return; end; 'LANGUAGE 'plpgsql'; regression=# select * from testarray(2,3); testarray ----------- {1} {1,1} {1,1,1} {2} {2,2} {2,2,2} (6 rows) HTH, Joe