Re: how to use array with "holes" ? - Mailing list pgsql-general

From Anton
Subject Re: how to use array with "holes" ?
Date
Msg-id 8cac8dd0706010135i2f28f7dfw9c5f4e2e7ca86480@mail.gmail.com
Whole thread Raw
In response to Re: how to use array with "holes" ?  ("Pavel Stehule" <pavel.stehule@gmail.com>)
Responses Re: how to use array with "holes" ?  ("Pavel Stehule" <pavel.stehule@gmail.com>)
List pgsql-general
> you have to initialise array before using. Like:
>
> declare a int[] = '{0,0,0,0,0, .................}';
> begin
>  a[10] := 11;

Ok, I got it, thanks! Now I can work with simle arrays.

May I ask some more complex? I want to use ONE multidimensial array -
the "id", "bytes_in" and "bytes_out". By another words, I need an
array, each element of which must contain 3 values: ttc_id, bytes_in,
bytes_out.

I think it can be done like this:

CREATE OR REPLACE FUNCTION myf_test() RETURNS void
AS $$
DECLARE
 p_tmp RECORD;
 p_last_cpnt RECORD;
 p_bytes bigint[][][][];
 i int;
BEGIN

 SELECT * INTO p_last_cpnt FROM nn_cpnt WHERE account_id = 5 ORDER BY
date_time DESC, cpnt_id DESC LIMIT 1;
 IF FOUND THEN
  FOR p_tmp IN SELECT ttc_id, bytes_in, bytes_out FROM nn_cpnt_traf
WHERE cpnt_id = p_last_cpnt.cpnt_id ORDER BY ttc_id LOOP
   --RAISE NOTICE 'ttc_id[%] -> in[%] out[%]', p_tmp.ttc_id,
p_tmp.bytes_in, p_tmp.bytes_out;
  -- get the next number for array index, ugly but works
   i = array_upper(p_bytes, 1) + 1;
   IF i IS NULL THEN
    i := 0;
   END IF;
   -- here I try to add element
   p_bytes[i] := ARRAY[p_tmp.ttc_id,p_tmp.bytes_in,p_tmp.bytes_out];
  END LOOP;
 END IF;
 -- ... some work. and here is the beauty of multidimensial. As I think...
 FOR i IN COALESCE(array_lower(p_bytes,1),1) ..
COALESCE(array_upper(p_bytes,1),-1) LOOP
   RAISE NOTICE 'p_bytes[%] = [%] / [%] / [%]', i, p_bytes[i][0],
p_bytes[i][1], p_bytes[i][2];
 END LOOP;
END;
$$
LANGUAGE plpgsql;

But I make an error somewhere, again...
=# SELECT myf_test();
ERROR:  invalid input syntax for integer: "{1,1907262814,9308877139}"
CONTEXT:  PL/pgSQL function "myf_test" line 18 at assignment

I think I not understand somewhat...
--
engineer

pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: user restriction
Next
From: "Dmitry Koterov"
Date:
Subject: Does slonik EXECUTE SCRIPT call waits for comands termination?