Thread: push array to array

push array to array

From
Tjibbe
Date:
I would like to push an array at the end of an array. Like:
    '{{4,5},{8,3}}' + '{3,6}' > '{{4,5},{8,3},{3,6}}'

Is there an easy way to do? (i have version "9.1.23" on my shared hosting)

Now I have written a complex function with EXECUTE command-string...


CREATE OR REPLACE FUNCTION push_2d_array(numeric[],numeric[]) RETURNS numeric[] AS $$
DECLARE
_str text;
_arr numeric[];
BEGIN
IF $1 IS NULL THEN
RETURN ARRAY[$2];
ELSE
_str := 'SELECT ARRAY[';
FOR i in 1..array_upper($1,1) LOOP
IF i > 1 THEN _str := _str || ','; END IF;
_str := _str || 'ARRAY[' || $1[i][1] || ',' || $1[i][2] || ']';
END LOOP;
_str := _str || ', ARRAY[' || $2[1] || ',' || $2[2] || ']]';
EXECUTE _str INTO _arr ;
RETURN _arr;
END IF;
END
$$ LANGUAGE plpgsql;




--
+31 6 29401726
tjibbe@rijpma.org

Re: push array to array

From
Pavel Stehule
Date:
Hi


2016-09-18 18:46 GMT+02:00 Tjibbe <tjibbe@rijpma.org>:
'{{4,5},{8,3}}' + '{3,6}'

postgres=# select '{{4,5},{8,3}}'::int[] || ARRAY[[3,6]];
+---------------------+
|      ?column?       |
+---------------------+
| {{4,5},{8,3},{3,6}} |
+---------------------+
(1 row)


regards

Pavel

Re: push array to array

From
Pavel Stehule
Date:


2016-09-18 19:12 GMT+02:00 Pavel Stehule <pavel.stehule@gmail.com>:
Hi


2016-09-18 18:46 GMT+02:00 Tjibbe <tjibbe@rijpma.org>:
'{{4,5},{8,3}}' + '{3,6}'

postgres=# select '{{4,5},{8,3}}'::int[] || ARRAY[[3,6]];
+---------------------+
|      ?column?       |
+---------------------+
| {{4,5},{8,3},{3,6}} |
+---------------------+
(1 row)


CREATE OR REPLACE FUNCTION public.array2d_append(integer[], integer[])
 RETURNS integer[]
 LANGUAGE sql
AS $function$
SELECT COALESCE($1 || ARRAY[$2], $1, $2);
$function$

postgres=# SELECT array2d_append('{{4,5},{8,3}}', '{3,6}');
+---------------------+
|   array2d_append    |
+---------------------+
| {{4,5},{8,3},{3,6}} |
+---------------------+
(1 row)


 

regards

Pavel

Re: push array to array

From
Pavel Stehule
Date:


2016-09-18 19:15 GMT+02:00 Pavel Stehule <pavel.stehule@gmail.com>:


2016-09-18 19:12 GMT+02:00 Pavel Stehule <pavel.stehule@gmail.com>:
Hi


2016-09-18 18:46 GMT+02:00 Tjibbe <tjibbe@rijpma.org>:
'{{4,5},{8,3}}' + '{3,6}'

postgres=# select '{{4,5},{8,3}}'::int[] || ARRAY[[3,6]];
+---------------------+
|      ?column?       |
+---------------------+
| {{4,5},{8,3},{3,6}} |
+---------------------+
(1 row)


CREATE OR REPLACE FUNCTION public.array2d_append(integer[], integer[])
 RETURNS integer[]
 LANGUAGE sql
AS $function$
SELECT COALESCE($1 || ARRAY[$2], $1, $2);
$function$

sorry

better

SELECT COALESCE($1 || ARRAY[$2], $1, ARRAY[$2]);
 

postgres=# SELECT array2d_append('{{4,5},{8,3}}', '{3,6}');
+---------------------+
|   array2d_append    |
+---------------------+
| {{4,5},{8,3},{3,6}} |
+---------------------+
(1 row)


 

regards

Pavel