Appending to an array

Started by Jay O'Connorabout 23 years ago4 messagesgeneral
Jump to latest
#1Jay O'Connor
joconnor@cybermesa.com

All,

I've got a tablewhere one of the columns contains an array. What I'm
trying to do is to append or insert into the array. I know I can so
something like..
UPDATE mytable set myarray[10] = 'some value' WHERE...

..to change a particular array element, and this be used to extend the
array if the array is not that size yet, but what I need to figure out is
how to do that when I don't know the size of the array at any given time.

Thanks

Jay

#2Oleg Bartunov
oleg@sai.msu.su
In reply to: Jay O'Connor (#1)
Re: Appending to an array

If you work with array of integers you may try our contrib/intarray
module:

OPERATIONS:

int[] && int[] - overlap - returns TRUE if arrays has at least one common ele
ments.
int[] @ int[] - contains - returns TRUE if left array contains right array
int[] ~ int[] - contained - returns TRUE if left array is contained in right
array
# int[] - return the number of elements in array
int[] + int - push element to array ( add to end of array)
int[] + int[] - merge of arrays (right array added to the end of left one)
int[] - int - remove entries matched by right argument from array
int[] - int[] - remove right array from left
int[] | int - returns intarray - union of arguments
int[] | int[] - returns intarray as a union of two arrays
int[] & int[] - returns intersection of arrays
int[] @@ query_int - returns TRUE if array satisfies query (like '1&(2|3)')
query_int ~~ int[] - -/-

Oleg
On Tue, 15 Apr 2003, Jay O'Connor wrote:

All,

I've got a tablewhere one of the columns contains an array. What I'm
trying to do is to append or insert into the array. I know I can so
something like..
UPDATE mytable set myarray[10] = 'some value' WHERE...

..to change a particular array element, and this be used to extend the
array if the array is not that size yet, but what I need to figure out is
how to do that when I don't know the size of the array at any given time.

Thanks

Jay

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Regards,
Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

#3Jay O'Connor
joconnor@cybermesa.com
In reply to: Oleg Bartunov (#2)
Re: Appending to an array

If you work with array of integers you may try our contrib/intarray
module:

Unfortunately, it's mostly arrays of strings.

Thanks

Jay

#4Joe Conway
mail@joeconway.com
In reply to: Jay O'Connor (#1)
Re: Appending to an array

Jay O'Connor wrote:

I've got a tablewhere one of the columns contains an array. What I'm
trying to do is to append or insert into the array. I know I can so
something like..
UPDATE mytable set myarray[10] = 'some value' WHERE...

..to change a particular array element, and this be used to extend the
array if the array is not that size yet, but what I need to figure out is
how to do that when I don't know the size of the array at any given time.

How's this? (requires version 7.3.x)

CREATE OR REPLACE FUNCTION array_next(text[]) returns int AS '
DECLARE
arr alias for $1;
high int;
BEGIN
high := 1 +
replace(split_part(array_dims(arr),'':'',2),'']'','''')::int;
RETURN high;
END;
' LANGUAGE 'plpgsql' IMMUTABLE STRICT;

create table mytable (myarray text[]);
insert into mytable values ('{"abc","d e f"}');
update mytable set myarray[array_next(myarray)] = 'new element';
regression=# select * from mytable;
myarray
-----------------------------
{abc,"d e f","new element"}
(1 row)

HTH,

Joe