I am trying to transform a PostgreSQL text array into one where each value is replaced by one where each white space is surrounded by a character on each side. In other words, I am trying to apply trim()
and regexp_replace()
on each value in a text array. This is all done (among other things) inside a database function.
CREATE OR REPLACE FUNCTION manipulate_array(multiplevalues text[])
RETURNS text[] AS
$BODY$
DECLARE
singlevalue text;
BEGIN
FOREACH singlevalue IN ARRAY multiplevalues LOOP
SELECT trim(regexp_replace(singlevalue, '\s+', ' ', 'g')) INTO singlevalue;
END LOOP;
RETURN multiplevalues;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION manipulate_array(multiplevalues text[]) OWNER TO username;
Unfortunately, when I call the function with multiplevalues = '{" red ", " blue ", " Ye llow "}'
as one of the arguments the returned value is the exact same text array. How do I get '{"red", "blue", "yellow"}'
as a return value?
I have been staring at the definitions for trim()
, regexp_replace()
and the FOREACH
loop for a while now, and I might just need a check from someone else.