Skip to main content

Posts

Showing posts from September, 2010

COBOL Tutorial 00300 – Edited Fields

As I have mentioned in the last tutorial, you use edited fields in COBOL to format data fields into human-readable display strings. Let’s start with a numeric field:01 NUMERIC-FIELD PIC 999999V99.and some COBOL code that set and display the field value:MOVE 1234.5 TO NUMERIC-FIELD. DISPLAY NUMERIC-FIELD: ' NUMERIC-FIELD.As we’ve demonstrated in the previous tutorial, unused digits are padded with ugly zeros:NUMERIC-FIELD: 001234.50Let me put my C# programmer hat on again (apologies to Java, ruby, python, C/C++, assembly and many other programmers who don’t like C#), when we have to format a variable for display, we often use the string.Format method with a formatting string containing special formatting characters, which is “0,0.00” in the following example:// returns 1,234.50 string.Format("{0:0,0.00}", 1234.5)Now let’s come back to COBOL, an edited field is basically a normal COBOL data field with a formatting string in the picture clause instead of the “A”, “X” or “9”…

COBOL Tutorial 000200 – Data Fields

Variables are called Fields in COBOL and definitions of variables are declared in the Picture clause (can be abbreviated with PIC). Why is it called the Picture Clause? According to the book Sams Teach Yourself COBOL in 24 Hours, this is because it “paints a picture of how a field looks by defining every details and characteristic of the field”, still doesn’t quite make sense to me but anyway.Let’s start by talking about what data fields (variables) look like in C#. When we declare a variable, the first thing we have to think about is the data type, which determines what kind of data can it hold. Normally we wouldn’t worry about the number of digits or length of the string unless we know their values can get ridiculously large or long.int integerVariable = 12345678; string stringVariable = "abcd1234"; decimal decimalVariable = 1234.5678m;In a COBOL world, however, the size does matter and you have to specify both the type and the size for each data field at the same time wit…

COBOL Tutorial 000100 – the ‘Hello World’

Due to a new project at work, I'm starting to learn COBOL (not the sexiest language, I know). I found the hardest part about learning COBOL for me is to know what COBOL keywords mean in terms of more modern programming languages. Therefore, I thought I’ll write a couple of short tutorials here to explain some of these differences in case some other programmers are interested in learning this 50+ years old programming language.As with learning any other programming language, the first example has to be the “Hello World” and here’s the source code: IDENTIFICATION DIVISION. PROGRAM-ID. HELLOWORLD. PROCEDURE DIVISION. DISPLAY 'HELLO WORLD'. STOP RUN.Every COBOL program needs an IDENFICATION DIVISION and the PROGRAM-ID (which is HELLOWORLD in our example). All program logic will sit under the PROCEDURE DIVISION. The rest of the program should be pretty self-explanatory.The full stop (.) is the equivalent of semi-colon (;) in C-derived…