Re: Setting variables equal to elements from an Array - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: Setting variables equal to elements from an Array
Date
Msg-id 20080110145206.GC29714@svana.org
Whole thread Raw
In response to Re: Setting variables equal to elements from an Array  (cdecarlo <cdecarlo@gmail.com>)
List pgsql-general
On Wed, Jan 09, 2008 at 06:14:10AM -0800, cdecarlo wrote:
> Maybe, an example will help you understand what I want to do:
>
> Let myArray be {{1,2,3},{4,5,6},{7,8,9}} and suppose the element I'm
> looking for has, in it's first index, an even number. I would loop
> through myArray looking at the first index of each element and when I
> got to index 2 of myArray I would have found an element which meets
> that criteria.  How would I set myVar equal to the second element of
> myArray?

Firstly, a 2-D is not an array of arrays. Think matlab not C.

> When i is 2, does myVar := myArray[i] set myVar equal to {4,5,6}?

kleptog=# select '{{1,2,3},{4,5,6},{7,8,9}}'::_int4 as test into temp a;
SELECT
kleptog=# select * from a;
           test
---------------------------
 {{1,2,3},{4,5,6},{7,8,9}}
(1 row)

kleptog=# select test[2] from a;
 test
------

(1 row)

So the answer is no.

> OR
>
> When i is 2, does myVar := myArray[i][3:3] set myVar equal to {4,5,6}?
kleptog=# select test[2][3:3] from a;
   test
-----------
 {{3},{6}}
(1 row)

So no again.

I think what you want is:
kleptog=# select test[2:2][1:3] from a;
   test
-----------
 {{4,5,6}}
(1 row)

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
>  -- John F Kennedy

Attachment

pgsql-general by date:

Previous
From: Erik Jones
Date:
Subject: Re: vacuum, dead rows, usual solutions didn't help
Next
From: "Merlin Moncure"
Date:
Subject: Re: Connect to SQL Server via ODBC from Postgresql