Multidimentional array access

Started by VENKTESH GUTTEDARover 9 years ago4 messagesgeneral
Jump to latest
#1VENKTESH GUTTEDAR
venkteshguttedar@gmail.com

Hello,

I want to get the inner array in a multi dimentional array in a
pl/pgsql procedure.

Eg : {{1,2,3,4,5,6,7},{11,22,33,44,55,66,77}}

for i in array_lower(Eg, 1) .. array_upper(Eg, 1)
LOOP
array_value = Eg[i]
END LOOP;

But Eg[i] is assigning null to array_value

Help would be appreciated.

--
Regards :
Venktesh Guttedar.

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: VENKTESH GUTTEDAR (#1)
Re: Multidimentional array access

Hi

2016-12-09 16:05 GMT+01:00 VENKTESH GUTTEDAR <venkteshguttedar@gmail.com>:

Hello,

I want to get the inner array in a multi dimentional array in a
pl/pgsql procedure.

Eg : {{1,2,3,4,5,6,7},{11,22,33,44,55,66,77}}

for i in array_lower(Eg, 1) .. array_upper(Eg, 1)
LOOP
array_value = Eg[i]
END LOOP;

But Eg[i] is assigning null to array_value

Help would be appreciated.

postgres=# do
$$
declare i int; j int; a int[];
begin
a := ARRAY[[1,2],[3,4]];
for i in array_lower(a,1) .. array_upper(a,1)
loop
for j in array_lower(a,2)..array_upper(a,2)
loop
raise notice 'a[%,%]=%', i,j,a[i][j];
end loop;
end loop;
end;
$$;
NOTICE: a[1,1]=1
NOTICE: a[1,2]=2
NOTICE: a[2,1]=3
NOTICE: a[2,2]=4
DO
Time: 142,078 ms

What is your PostgreSQL version?

Regards

Pavel

Show quoted text

--
Regards :
Venktesh Guttedar.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: VENKTESH GUTTEDAR (#1)
Re: Multidimentional array access

VENKTESH GUTTEDAR <venkteshguttedar@gmail.com> writes:

I want to get the inner array in a multi dimentional array in a
pl/pgsql procedure.

Eg : {{1,2,3,4,5,6,7},{11,22,33,44,55,66,77}}

for i in array_lower(Eg, 1) .. array_upper(Eg, 1)
LOOP
array_value = Eg[i]
END LOOP;

But Eg[i] is assigning null to array_value

I think you want

array_value = Eg[i][array_lower(Eg, 2):array_upper(Eg, 2)]

As of 9.6 you could use the shorthand

array_value = Eg[i][:]

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

In reply to: Tom Lane (#3)
Re: Multidimentional array access

On 09/12/16 15:30, Tom Lane wrote:

As of 9.6 you could use the shorthand

array_value = Eg[i][:]

regards, tom lane

I hadn't spotted that - very handy - will go and read up on it. :-)

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general