Thread: plpgsql code doen't work

plpgsql code doen't work

From
janek12@web.de
Date:
Hi, 
 
I found following code:

create or replace function plpgsql_edit_distance(stra text, strb text)
    returns integer as $$
declare
    rows integer;
    cols integer;
begin
    rows := length(stra);
    cols := length(strb);

    IF rows = 0 THEN
        return cols;
    END IF;
    IF cols = 0 THEN
    return rows;
    END IF;

    declare
    row_u integer[];
    row_l integer[];
    diagonal integer;
    upper integer;
    left integer;
    begin
    FOR i in 0..cols LOOP
        row_u[i] := i;
    END LOOP;

    FOR i IN 1..rows LOOP
        row_l[0] := i;
        FOR j IN 1..cols LOOP
            IF substring (stra, i, 1) = substring (strb, j, 1) THEN
            diagonal := row_u[j-1];
        else
            diagonal := row_u[j-1] + 1;
        END IF;
        upper := row_u[j] + 1;
        left := row_l[j-1] + 1;
        row_l[j] := int4smaller(int4smaller(diagonal, upper), left);
        END LOOP;
            row_u := row_l;
    END LOOP;
    return row_l[cols];
    end;
end
$$ language 'plpgsql' immutable strict;

Does anyone know why the colums :"row_l[j] := int4smaller(int4smaller(diagonal, upper), left);" doesn't work.
 
Janek Sendrowski


Re: plpgsql code doen't work

From
Beena Emerson
Date:
Hello,

Try changing the variable left to something other like left_val. It will work.
Maybe the problem is because LEFT is a keyword.


Beena Emerson




Re: plpgsql code doen't work

From
Giuseppe Broccolo
Date:
Il 10/09/2013 10:46, Beena Emerson ha scritto:
> Hello,
>
> Try changing the variable left to something other like left_val. It
> will work.
> Maybe the problem is because LEFT is a keyword.
Yes, left() is a function returning a 'text'. There's a conflict when
you define it as an 'integer'...

Giuseppe.

--
Giuseppe Broccolo - 2ndQuadrant Italy
PostgreSQL Training, Services and Support
giuseppe.broccolo@2ndQuadrant.it | www.2ndQuadrant.it