TechTip: Incrementing an Alphanumeric Number

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

"Why use an alphanumeric number rather than just a regular old number?" I asked. The requirement for an alphanumeric order number was written into an e-commerce spec on the project I was assigned to develop. The answer was a good one: volume. The Web site owners wanted to maintain a four-digit order number but didn't want to be limited to 10,000 orders (0000-9999). They were being optimistic about the store's volume.

So I did the math. The common numbering system is base 10, meaning that any single digit could be 0 through 9, so a four-digit number is 10 to the 4th power or 10,000 (0 through 9999). An alphanumeric number of the same length--four digits--would be base 36, or A through Z plus 0 through 9. So an alphanumeric four-digit number has 1.67 million possible order numbers.

There is, however, a potential problem with using an alphanumeric order number--namely, unintended spellings like "WE8U" (we hate you), "UASS," or the numerous other four letter words that would eventually show up. Use your imagination. The solution we settled on was to remove all vowels: a,e,i,o, and u. This had the added benefit of removing the possible confusion of the letter O with number 0.

For the example I'm showing here, I'm using a base 31, alphanumeric, four-digit number, which gives me 31 to the 4th power, or just under a million potential order numbers (923,521 to be exact).

I wrote it as a function in which you pass the "current" order number and get back the next order number in the sequence. You would still need to store your current "high" number somewhere.

REM Open the function and pass in the old number
function GetNextOrderNumber(Old_Number)
     
maxOrderID = Old_Number
Dim alphaNum
Dim alphaNumList
alphaNumList = "0,1,2,3,4,5,6,7,8,9,B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z"

REM Note that the alphaNumList does not contain the vowels.

REM Split the alphanumList into an array
alphaNum = split(alphaNumList,",")

REM Parse the current order number into an array.
Dim F(4)
F(0) = Right(Left(maxOrderID,1),1)
F(1) = Right(Left(maxOrderID,2),1)
F(2) = Right(Left(maxOrderID,3),1)
F(3) = Right(Left(maxOrderID,4),1)

REM  Loop thru each position starting with the last digit.
For Y = 3 to 0 Step -1
REM If the last position is Z then roll it over to 0
If  F(Y) = "Z" then
F(Y) = "0"
else
For X = 0 to 30   
If F(Y) = alphaNum(X) then
  NewF = alphaNum(X+1)
End If
Next 
F(Y) = NewF
exit for
End if
Next
REM Rebuild the new order number from the array
newOrderID = F(0) & F(1) & F(2) & F(3)  

REM Return the new order number
GetNextOrderNumber = newOrderID

end function



That's it--a simple function to convert a four-digit number from 10,000 order numbers to just under 1 million potential order numbers. By the same logic, a six-digit number will give you 887 million potential order numbers. If we get 887 million orders in our store, I'm demanding a percentage and retiring.

Good luck and good coding.

--Robin G. Grimes
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: