Thread: [GENERAL] Multidimentional array access

[GENERAL] Multidimentional array access

From
VENKTESH GUTTEDAR
Date:
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.

Re: [GENERAL] Multidimentional array access

From
Pavel Stehule
Date:
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

 


--
Regards :
Venktesh Guttedar.


Re: [GENERAL] Multidimentional array access

From
Tom Lane
Date:
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


Re: [GENERAL] Multidimentional array access

From
Raymond O'Donnell
Date:
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