Re: Need UPPER( ) function
Posted in 1996
Well I think we've finally got an upper function that works, although
the performance is questionable, especially if you want to search with
it. I added a couple of changes that I thought would improve it, at
least for my installations.
The first change is the "returning" clause where I used a varchar. I
thought this might be more efficient by returning only the "meat" of the
string instead of a full 255 bytes every time.
The second change concerns "foreign" languages where other letters than
"a" through "z" have to be upshifted. These letters are scattered about
the 8-bit ascii table.
Here's my iteration, compacted:
drop procedure upper;
create procedure upper(str char(255)) returning varchar(255);define retstr varchar(255);
define x char(1); -- sorry, I didn't like "char"
define i,len smallint; -- "l" is too much like i and nr. "1"
let retstr = '';
let len = length(str);
for i = 1 to len
if str[1,1] between "a" and "z" then
if str[1,1] = "a" then let x = "A";
elif str[1,1] = "b" then let x = "B";
... The english alphabet ...
elif str[1,1] = "y" then let x = "Y";
else let x = "Z"; -- no need to test for "z"
end if
elif str[1,1] = "'" then let x = "'";
... The rest of the alphabet ...
else let x = str[1,1];
end if
if i = 1 then let retstr = x;
else let retstr = retstr || x;
end if
let str = str[2,255];
end for
return retstr;
end procedure;
Robert Minter wrote:
>
> ... n'merous iterations of the UPPER() function (snipped) ...
> DROP PROCEDURE upper ;
> CREATE PROCEDURE upper (str CHAR(255)) RETURNING CHAR(255);>
> DEFINE i INTEGER;
> DEFINE l INTEGER;
> DEFINE retstr VARCHAR(255);
> DEFINE char CHAR;
>
> LET l = LENGTH(str);
>
> LET retstr = '';
> FOR i = 1 to l
> IF str[1] MATCHES "[a-z]" THEN
> IF str[1] = 'a' THEN
> LET char = 'A';
> ELIF str[1,1] = b' THEN
> LET char = 'B';
> ....
> ELIF str[1,1] = 'z' THEN
> LET char = 'Z';
> END IF
> ELSE
> LET char = retstr[1];
> END IF
>
> IF i = 1 THEN
> LET retstr = char;
> ELSE
> LET retstr = retstr || char;
> END IF
>
> LET str = str[2,255];
> END FOR
>
> RETURN retstr;
>
> END PROCEDURE;
----------------------------------------------------------------------
John H. Frantz Power-4gl: Extending Informix-4gl
frantz@centrum.is http://www.strengur.is/~frantz/pow4gl.html