HomePage | Optical Illusions | War Stories | QBasic | Dads Navy Days | Bristol | Bristol, USA | Bristol, Canada | Terre Haute | Miscellany | Web Stuff | About Ray | Site Map | Site Search | Messages | Credits | Links | Web Rings

QBasic | Errors | 40lb Weight | Bits | Chance | Colours | Dates | Delays | File Dialog | Files | Input | Matching | Menus | Mouse | Numbers | SeqNo | SIRDS | Sorts | Text | Timer | DLoads

Sequence Numbers - Numbers as Strings

Every record in every data file that leaves our processing department gets assigned a sequence number. The reasons for this are that it allows us to find records quickly should we need to and also because our work is output to printers where the reels of paper sometimes tear or need changing, it simplifies the work of the operators finding the place in the file to restart from.

We use SDFs (System Data File), which are fixed record length files. This means that the file contains records that have the data left justified and padded out with spaces within the fields, which may have varying lengths through a record, but remain the same within the database. The records end with a CRLF. Basically this means that if the file is opened in a text editor then the data will be arranged in columns.

Our sequence numbers are fixed length fields of 7 characters which allows us to easily process databases of 1 less than 10 million records.

For our purposes, the numbers are right justified within a field of seven characters and padded out with "0"s. This can be done in one line of code inside a loop :-

OutRec$ = STRING$(7 - LEN(LTRIM$(STR$(Count))), "0") + LTRIM$(STR$(Count)) + InRec$

Well, what does all this mean ?

OutRec$ is a variable that holds the record that will be output to a new file
InRec$ is a variable that holds a record from a data file
Count is a numeric variable that forms part of the loop counter

The line of code converts the numeric Count to a string, STR$(Count). When QBasic does this operation for some reason it adds a space to the front of the string, this is stripped out by using LTRIM$. LEN is the function that returns the length of a string. STRING$ is a function that will return a character, in this case "0", a given number of times. In this case, it is 7 minus the length of the string representation of Count.

The piece of code then adds the string representation of Count (minus the leading space) to OutRec$ then, lastly, the string InRec$ is added.

The technique is demonstrated in the follwing program :-

'SeqNum   Ray Thomas    May 2000

'A program to demonstrate adding fixed length sequence numbers to a string

'Set up the variables
'********************

DIM Count AS INTEGER
DIM SeqNo AS INTEGER
DIM SeqLen AS INTEGER
DIM SeqStrt AS INTEGER
DIM InString AS STRING
DIM OutString AS STRING
DIM Pad AS STRING

InString$ = "Mary had a little lamb."

'Get the start number and number of items to be produced
'*******************************************************

CLS
PRINT
PRINT " A program to demonstrate adding a fixed length sequence to a string."
PRINT
INPUT ; "From what number do you want to start the sequence from"; SeqStrt
PRINT
INPUT ; "How many lines do you want to produce"; SeqNo
PRINT
INPUT ; "How long do you want the sequence number field to be"; SeqLen
PRINT
INPUT ; "What character do you want the sequence number padded out with"; Pad$

'Main program
'************

CLS
PRINT
FOR Count = SeqStrt TO SeqStrt + SeqNo - 1
   OutString$ = STRING$(SeqLen - LEN(LTRIM$(STR$(Count))), Pad$) + LTRIM$(STR$(Count)) + InString$
   PRINT OutString$
NEXT Count
END

As you can see, a program does not have to be long and complicated to be useful, but then again this does no error checking at all, it takes it on trust that the user knows amongst other things :-

If you are writing programs for your own use then such considerations can, usually, be ignored. Programs written for use by other people need far more thought and error checking built into them. See Errors for more details.

QBasic | Errors | 40lb Weight | Bits | Chance | Colours | Dates | Delays | File Dialog | Files | Input | Matching | Menus | Mouse | Numbers | SeqNo | SIRDS | Sorts | Text | Timer | DLoads

HomePage | Optical Illusions | War Stories | QBasic | Dads Navy Days | Bristol | Bristol, USA | Bristol, Canada | Terre Haute | Miscellany | Web Stuff | About Ray | Site Map | Site Search | Messages | Credits | Links | Web Rings