TechTip: Generate Unique Strings

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: