Code for review - Increment a character field

Chris Kelleher

Administrator
Staff member
Hi folks,

Here's a function I had to write today; it "increments" a character string
to generate the next one in sequence, the idea being to follow spreadsheet
column naming i.e. A-Z, AA-AZ etc. You specify the current value as an input
parameter to the function, and it returns the next one. I've not used
functions much, so all comments are welcome!

Cheers,
Nick

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>

FUNCTION next-code RETURNS CHAR (INPUT ip-code AS CHAR).

DEF VAR lv-a AS INT NO-UNDO.
DEF VAR lv-b AS INT NO-UNDO.

/* If being called for the first time, */
/* just return a letter "A". */

IF ip-code = "" THEN RETURN "A".

/* Starting at the right end of the string, */
/* look for a non-z character. If one is */
/* found, increment it and set any chars to */
/* the right of it to "A"s. */

code-loop:
DO lv-a = LENGTH(ip-code) TO 1 by -1:

IF SUBSTR(ip-code,lv-a,1) <> "Z" THEN DO:
SUBSTR(ip-code,lv-a,1) = CHR(ASC(SUBSTR(ip-code,lv-a,1)) + 1).
DO lv-b = lv-a + 1 TO LENGTH(ip-code):
SUBSTR(ip-code,lv-b,1) = "A".
END.
RETURN ip-code.
END.

END.

/* To get this far, the string must contain */
/* all "Z"s, so we need to increase the */
/* length of the string and set everything */
/* to "A"s. */

RETURN FILL("A",LENGTH(ip-code) + 1).

END FUNCTION.

[/code]
 

Chris Kelleher

Administrator
Staff member
Hi Nick,
Okay, I'll be brave and have a shot at it. And in my defence, its Sunday
night, the kids have finally gone to bed, and I'm drinking a nice red.

a) There is no validation of the input parameter.
When somebody runs this with '$' as input, what should happen?
b) The opening line of code [IF ip-code = "" then return "A"] is
redundant as the outer [DO ... END.] takes care of this
situation already.
c) The whole function assumes an ASCII world. This makes it a little
less multi-language. Another approach is to have a list of valid
characters in sequence eg, "ABCDE...XYZ" that could be preset based
on national characters oranything else for that matter.
d) The inner [DO...END] loop is not an optimal way of completing the
task. At this point you know that all the characters to the right
must be replaced by 'A', so you could use something like...
ASSIGN
lv-b = LENGTH(ip-code) - lv-a
SUBSTR(ip-code,lv-a + 1, lv-b) = FILL('A', lv-b)
.
e) You could also improve performance if you minimise the number of
function evaluations performed.
f) However, the best advice I could give would be to look at the problem
in a different way. As I see it, what is required is to render a number
in almost base-27 notation using the characters "A" - "Z". The tricky
bit
is that there is no 'zero' character. So try to devise a function that
accepts an integer greater than 0 and returns the integer as a
character
string based on the character set [A...Z] .
 

Chris Kelleher

Administrator
Staff member
Nick,

You may want to look at kbase #16013 (How to Increment an Alphabetic Field).

Brian K. Maher
-------------------------------------------------------------------------
Principal Technical Support Engineer Phone : 781-280-3075
Progress Software E-Mail: maher@bedford.progress.com

For memory always lingers, to caress with cold fingers,
Frozen images long since gone;
Along these paths we tread, sometimes with cold dread,
To mar the night in the baited breath of dawn.
-------------------------------------------------------------------------
 
Top