Thread: Problem with the use of Array and Loop

Problem with the use of Array and Loop

From
"Benjamin RICHARD"
Date:
Hi,
I would like to read each value of an array.
To do this i create the following function which return me a parse error
near '[' (in the Raise notice line)
What mistake did i do ?
And maybe there is a better way to do this in Postgre

CREATE FUNCTION array_int(_int4) RETURNS int4 AS 'DECLARE
    start_char INTEGER DEFAULT 4;
    len_char INTEGER;
    array_count INTEGER;
    val_tmp VARCHAR;
    tmp INTEGER;
BEGIN
SELECT INTO val_tmp array_dims( $1 ) ; --get the length of the array =>
[x:y]
SELECT INTO len_char ( position('']'' in val_tmp) - start_char ) ; --get the
length of y
SELECT INTO array_count substring(val_tmp from start_char for len_char)
; --get the value of y


FOR tmp IN 1..(array_count) LOOP
    RAISE NOTICE ''current value = %'' , $1[tmp] ;
END LOOP;

RETURN array_count ;

END;
-------------------------------------------------------------------
Try :
select array_int('{5,3,6,9,7,5,10,5,87,45,12,65,98,21,65,21}') ;


Re: Problem with the use of Array and Loop

From
Tom Lane
Date:
"Benjamin RICHARD" <benjamin@intradot.com> writes:
> To do this i create the following function which return me a parse error
> near '[' (in the Raise notice line)
> What mistake did i do ?

IIRC, the RAISE NOTICE command is incapable of evaluating expressions;
it can only handle trivial variable references.  Do something useful
with the array element instead of RAISE ...

            regards, tom lane