Search Postgresql Archives

Referring to function parameter in function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I appear to be having a problem with a function I've created, and no
doubt it'll be something obvious I'm doing wrong.  Here's my function:

CREATE OR REPLACE FUNCTION get_lsfr(
	bitlength INT,
	taps INT[],
	from_value INT
) RETURNS INT AS $$
DECLARE
	last_tap_value BIT;
	tap INT;
	new_value INT;
BEGIN	
	IF (SELECT MAX(x) FROM unnest(taps) AS x) > bitlength THEN
		RAISE EXCEPTION 'LSFR tap exceeds range of value.';
	END IF;

	FOR tap IN SELECT value FROM unnest(taps) AS x(value) ORDER BY value DESC LOOP
		IF last_tap_value IS NOT NULL THEN
			last_tap_value := last_tap_value #
GET_BIT(from_value::bit(bitlength), tap.value-1);
		ELSE
			last_tap_value := GET_BIT(from_value::bit(bitlength), tap.value-1);
			CONTINUE;
		END IF;
	END LOOP;

	new_value := (last_tap_value || SUBSTRING(from_value::BIT(bitlength),
1, bitlength - 1))::BIT(bitlength)::INT;

	RETURN new_value;
END;
$$ LANGUAGE plpgsql;

And here's it's usage and result:

select get_lsfr(4,'{3,4}'::int[],6);
ERROR:  invalid input syntax for integer: "bitlength"
LINE 1: SELECT GET_BIT(from_value::bit(bitlength), tap.value-1)
                                   ^
QUERY:  SELECT GET_BIT(from_value::bit(bitlength), tap.value-1)
CONTEXT:  PL/pgSQL function "get_lsfr" line 14 at assignment

If the function is difficult to read, please look at this paste:
http://pgsql.privatepaste.com/fd5b83166c

I want to use the parameter called "bitlength" as the length of a bit
when casting a value.

So, in this case, it would be GET_BIT(6::bit(4), 4-1)

What am I missing?

-- 
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux