Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 77DD2632942 for ; Thu, 25 Mar 2010 13:00:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 30626-01 for ; Thu, 25 Mar 2010 16:00:29 +0000 (UTC) X-Greylist: delayed 00:43:31.068706 by SQLgrey-1.7.6 Received: from mail42.e.nsc.no (mail42.e.nsc.no [193.213.115.42]) by mail.postgresql.org (Postfix) with ESMTP id 936CD632805 for ; Thu, 25 Mar 2010 13:00:27 -0300 (ADT) Received: from balapapa.localnet (42.185.16.62.customer.cdi.no [62.16.185.42]) (authenticated bits=0) by mail42.nsc.no (8.14.3/8.14.3) with ESMTP id o2PG0PlP026418 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Thu, 25 Mar 2010 17:00:26 +0100 (MET) From: Leif Biberg Kristensen To: postgresql Subject: Re: Plpgsql: Iterating through a string of parameters Date: Thu, 25 Mar 2010 17:00:25 +0100 User-Agent: KMail/1.12.4 (Linux/2.6.27-gentoo-r10; KDE/4.3.5; i686; ; ) References: <201003251616.53097.leif@solumslekt.org> In-Reply-To: <201003251616.53097.leif@solumslekt.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201003251700.25623.leif@solumslekt.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/186 X-Sequence-Number: 33893 On Thursday 25. March 2010 16.16.53 Leif Biberg Kristensen wrote: > I'm struggling with how to make plpgsql iterate through a list of numbers > input as a text string, eg. "1438 2656 973 4208". I figure that I can use the > regexp_split_to_array() function to make an array of the string, but can I > iterate through an array with eg. a FOR loop? I found a solution myself through trial-and-error: CREATE OR REPLACE FUNCTION text_to_arr(TEXT) RETURNS VOID AS $$ DECLARE arr TEXT ARRAY; BEGIN arr := regexp_split_to_array($1, E'\\s+'); FOR i IN 1..array_length(arr, 1) LOOP RAISE NOTICE '%', arr[i]::INTEGER; END LOOP; RETURN; END $$ LANGUAGE plpgsql IMMUTABLE; pgslekt=> select * from text_to_arr('1438 2607 1504'); NOTICE: 1438 NOTICE: 2607 NOTICE: 1504 text_to_arr ------------- (1 row) pgslekt=> regards, -- Leif Biberg Kristensen http://solumslekt.org/