REBOL for COBOL programmers

Go to table of contents Go to feedback page

nachalist global services

Date written: October 17, 2012, 2012
Date revised:
Date reviewed:

This page describes some modules that the nachalist program will use, and that can be put into the global services module so that other programs can use them also.


General plan of action

This is something you almost certainly have done in COBOL, not because it is a COBOL-specific thing to do, but because it makes sense for any programming language. You run into something that you do over and over again, and you encapsulate that code into some sort of module that you can re-use. The above page called "your own mezzanine" refers to this concept.

If we were explaining these examples in tutorial manner, we would go through the steps of development, encounter some of that code that could be encapsulated, and then make a module out of it, and then at some later time, say, "Aha, we should put all those common procedures into some kind of re-useable module." However, because these samples are presented to get you going faster, we will present here the modules needed in this sample, and then present the entire file of all such modules used in all samples. The other sample programs will be documented in a manner similar to this module, so that "Global services module" that contains all this code will be presented many times. this is so that you can jump into any sample and not be missing some key information presented in another sample.

Substring function

The data that this program will read is in fixed-format text, with no delimiters between fields. This is very common to COBOL programs, but foreign to REBOL. However, in REBOL, simple things are simple, but difficult things are possible. So, we need a way to take apart a COBOL-friendly fixed-format record.

There are a couple different ways to design a substring function. You could take a substring from a start position to an end position, you could take it from a start position for a given number of bytes. There is no "substring" function built into REBOL, but it is easy to make one. Here is one idea harvested from the rebol.org website.

substring: function  [

{ Expression function to get a substring from a string
  Usage: substring "abcdefghijk" 4 9
}
   s [series!] {String} 
   f [number!] {Position from}
   t [number!] {Position until, -1 when end of series}
]  [
]  [ (if t = -1 [ t: length? s])
      return skip (copy/part s t) (f - 1)
]

The above is "real" REBOL code, but, comforted by the wordiness of COBOL as I am, I modified it a bit:

GLB-SUBSTRING: func [
    "Return a substring from the start position to the end position"
    INPUT-STRING [series!] "Full input string"
    START-POS    [number!] "Starting position of substring"
    END-POS      [number!] "Ending position of substring"
] [
    if END-POS = -1 [END-POS: length? INPUT-STRING]
    return skip (copy/part INPUT-STRING END-POS) (START-POS - 1)
]

So how does it work. It takes three parameters, an input string, a starting position, and an ending position. If the ending position is -1, that is a code to use the end of the string as the ending position. The first thing it does is to check for that special case of the ending position being -1. If it is, it resets the ending position to the length of the string, which of course would be the character position of the end of the string.

The command

(copy/part INPUT-STRING END-POS)
copies the INPUT-STRING from its beginning to the character position specified by END-POS. This piece of the string is passed to the "skip" function. The "skip" function jumps into the string by a specified number of characters and returns the string from that point to the end. The starting position tells us how many, but we don't want to skip in by that many characters; we want a zero-relative number of characters, because the starting position of 1 would mean not to skip any characters at all. That is why the number of characters to skip is one less than the starting position, or (START-POS - 1). The result of the "skip" function is returned to the caller by the "return" command.

This function has been written up as you see it, with some comments, and put into our "Global Services Module" so it can be used in this sample program and other programs.

Code samples

Left-clicking a link below will give a result that will depend on your computer, your browser, and maybe in whether or not you have REBOL installed. You should get either a new window with the code displayed, or a dialog box for saving the file on your own computer, or "opening" the file which makes no sense in this situation. Right-clicking should give you a menu from which you can save it to your own computer.

Global services module