Re: Setting variables equal to elements from an Array

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: cdecarlo <cdecarlo(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Setting variables equal to elements from an Array
Date: 2008-01-10 14:52:06
Message-ID: 20080110145206.GC29714@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)svana(dot)org> http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
> -- John F Kennedy

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Erik Jones 2008-01-10 14:52:11 Re: vacuum, dead rows, usual solutions didn't help
Previous Message Magnus Hagander 2008-01-10 14:50:24 Re: After Installing a Program I get this error: psql:sql/Pg-database.sql:825: ERROR: language "plpgsql" does not exist