For/Next loop example

Here is a more detailed example of Seagate Crystal Reports' string processing capabilities. The Caesar cipher is a simple code that is traditionally credited to Julius Caesar. In this code, each letter of a word is replaced by a letter five characters further in the alphabet. For example, "Jaws" becomes "Ofbx". Notice that "w" is replaced by "b". Since there are not 5 characters after "w" in the alphabet, it starts again from the beginning.

Here is a formula that implements applying the Caesar cipher to the field {Customer.Customer Name} in the Xtreme database:

Rem The Caesar cipher

Dim inString 'The input string to encrypt
inString = {Customer.Customer Name}
Dim shift
shift = 5

formula = ""

Dim i
For i = 1 To Len(inString)
   Dim inC, outC
   inC = Mid(inString, i, 1)
   Dim isChar, isUCaseChar
   isChar = LCase(inC) In "a" To "z"
   isUCaseChar = isChar And (UCase (inC) = inC)

   inC = LCase(inC)

   If isChar Then
      Dim offset
      offset = (Asc(inC) + shift - Asc("a")) Mod _
               (Asc("z") - Asc("a") + 1)
      outC = Chr(offset + Asc("a"))
      If isUCaseChar Then outC = UCase(outC)
   Else
      outC = inC
   End If
   formula = formula & outC
Next i

In the above example, there is a multi-line If statement nested within the statements block of the For/Next loop. This If statement is responsible for the precise details of shifting a single character. For example, letters are treated differently from punctuation and spaces. In particular, punctuation and spaces are not encoded. Control structures can be nested within other control structures and multiple statements can be included in the statement block of a control structure.



Seagate Software, Inc.
http://www.seagatesoftware.com
Please send comments to:
techpubs@seagatesoftware.com