Re: Setting variables equal to elements from an Array

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
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-08 16:35:56
Message-ID: 10048.1199810156@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

cdecarlo <cdecarlo(at)gmail(dot)com> writes:
> I'm new to pl/pgsql and postgres and I need some help with a part of
> my function. In the function I loop through a multidemensional array
> ( [n][3] ), once, while inside the loop, I find the index I want to
> work with I would like to set a separate variable equal to the element
> in the array at that index ( newVar = array[i] ).

The rest of your message suggests that what you want is not that at all,
but to set the other variable to an array that consists of one or more
elements from the original array. You need to be clearer in your own
mind about which it is you're doing --- an array of one element is
completely different from the element itself.

If you use subscripts that include a colon (:), then the result is a
sub-array and has to be assigned to a variable that's of the array
type. If you don't use a colon then the subscripting operation produces
a single value that's of the array element type, and has to be assigned
to a variable of that type. You have to use the correct number of
subscripts in either case, else you get a NULL, as you found out.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Browne 2008-01-08 17:09:03 Re: large objects,was: Restoring 8.0 db to 8.1
Previous Message Tom Lane 2008-01-08 16:29:07 Re: need to dump/restore data for 8.3beta2 -> 8.3RC1 upgrade?