|
Find the Length of a Character String in CL
Hey, Ted:
Numerous times over the years, I've seen this question: How can I find the length of the value in a CL variable? That is, how many characters are in a variable, ignoring trailing blanks? I needed the answer often enough myself that I created my own "clever" way to handle it years ago by creating a RTNLEN command. But I recently discovered an alternative that's been around forever. It uses a single command, RTVMSG, and it works for character variables up to 512 bytes long.
Here's an example:
dcl &myvar *char 256 value('Abcd') /* Can be max *CHAR(512) */
dcl &l_myvar *dec (5 0)
rtvmsg msgid(CPF9897) msgf(QCPFMSG) msgdta(&myvar) msglen(&l_myvar)
And that's all there is to it. Use CPF9897 because it has no message text of its own--the returned length includes only the length of the supplied message data. It's far from perfect because of the length limitation; but for perhaps the majority of cases, it works very well.
--Tom Liotta, PowerTech
That's clever, Tom. Thanks for sending it in.
--Ted
|