Re: [Q] SPL code to scan a char(n) variable
Posted in 1997
Ian Bishrey wrote:
>
> Context:
> o IBM RS6k F40, AIX 4.1.5
> o Informix SE 7.13UC2
> o dbaccess
>
> Hi.
>
> I'm trying to write some SPL code to parse a DATETIME field which has been
> converted into a CHAR array. I want to do this kind of thing:
>
> for l_nIndex in ( 1 to 20 step 1 )
> if l_strDateTimeRenderedAsString[l_nIndex] == ' ' then
> {stuff}
> else
> {other stuff}
> end if
> end for
>
> i.e. I want to extract the Nth character from a string. The above code
> produces an error which implies that only constants are allowed within the
> [] construct. Can this be true? Is there another way of going about it ?
>
> TIA
>
> Ian
>
> --
>
> Ian Bishrey
> Systems Manager
> Kam Circuits Ltd, Station Road, Calne, Wilts, SN11 0JT. UK.
> http://www.kam.com
> Tel: +44 1249 815262
> Fax: +44 1249 814726
Hi!
Yes, that is true (an it stinks too). There is a W/O which you can
probably get from Informix (I once wrote it when I worked there), or you
can pick it up from this uncomplete code, which I havn't tested, but I
think you get the idea.
The basic idea is this:
Create a SPL PROC that takes, say a VARCHAR(255) as an argument and
returns a VARCHAR(254). This PROC returns characters 2 - # of chars in
the input string:
CRETATE PROCEDURE nexone(i VARCHAR(255))
RETURNING VARCHAR(254);
RETURN i[2,255];
END PROCEDURE;
Then you create a procedure that loops through this procedure n, number
of times:
CREATE PROCEDURE getchar(s VARCHAR(255), n INTEGER)
RETURNING CHAR(1);DEFINE i INTEGER;
FOR i = 1 TO n
LET s = nextone(s);
END FOR
RETURN s[1];
END PROCEDURE;
This code is written as I mail this doc, so there might be a type or
two, but this is the basic idea.
Rgds
Karlsson
--
====================================================================
Anders Hackin' Karlsson
Your friendly Database Guru
Email: andersk.karlsson@karlsson.pp.se
"Every time I've built character, I've regretted it" Calvin
====================================================================