If you want to generate many unique strings, you can easily use a zoned numeric subfield in a data structure and add 1 to the zoned field, giving you strings like A000, A001, up to A999.
But you can generate many more unique strings if you use more characters than just 0 to 9. If you add the alphabetic characters A to Z, in a 3-byte field, you can get 46,656 (36 cubed) different values rather than just 1,000 (10 cubed).
The technique is the same as the way you do addition by hand. Given the sequence of characters '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', say you want to "add one" to the string '01B'. You start with the rightmost "digit" ('B') and add one to it ('C'). This gives '01C'. If you start with 'RPZ', since 'Z' is the last character in your sequence, you can't add 1 to it, so you set it to '0' (the first character in the sequence) and move on to the second character ('P'). Adding one to 'P' gives 'Q', so your result is 'RQ0'.
The following program illustrates the technique.
D counters c '0123456789+ D ABCDEFGHIJKLMNOPQRSTUVWXYZ'
D string s 10a inz('000000ZZZW')
D i s 10i 0 D cur s 10i 0 D digit s 10i 0 D lastChar s 1a D firstChar s 1a D curChar s 1a /free firstChar = %subst(counters : 1 : 1); lastChar = %subst(counters : %len(counters) : 1);
for i = 1 to 10; // "add" 1 to the string 10 times
// this does the "adding" for digit = %len(string) downto 1; curChar = %subst(string : digit : 1); cur = %scan(curChar : counters); if cur = %len(counters); %subst(string : digit : 1) = firstChar; // we have to "carry the 1" to the next digit else; %subst(string : digit : 1) = %subst(counters : cur + 1 : 1); leave; // done; we have added the 1 to the current digit endif; endfor;
dsply string;
endfor;
*inlr = '1';
/end-free |
| Barbara Morris can be reached by email at bmorris@ca.ibm.com.
|
You must be logged in to view or make comments on this article.