Number Parsing in COBOL

Here’s the translation of the number parsing example from Go to COBOL, formatted in Markdown suitable for Hugo:

Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in COBOL.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. NUMBER-PARSING.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-FLOAT        PIC 9(3)V9(3).
       01 WS-INT          PIC S9(9) COMP.
       01 WS-UINT         PIC 9(9) COMP.
       01 WS-HEX          PIC X(6).
       01 WS-RESULT       PIC Z(8)9.

       PROCEDURE DIVISION.
           MAIN-PROCEDURE.
      * With MOVE, we can convert a string to a floating-point number
           MOVE "1.234" TO WS-FLOAT
           DISPLAY WS-FLOAT

      * For integer conversion, we use COMPUTE
           COMPUTE WS-INT = FUNCTION NUMVAL("123")
           MOVE WS-INT TO WS-RESULT
           DISPLAY WS-RESULT

      * COBOL doesn't have built-in hexadecimal parsing, so we'd need a custom routine
           MOVE "0001C8" TO WS-HEX
           CALL "CONVERT-HEX-TO-DEC" USING WS-HEX, WS-INT
           MOVE WS-INT TO WS-RESULT
           DISPLAY WS-RESULT

      * For unsigned integers, we use a similar COMPUTE
           COMPUTE WS-UINT = FUNCTION NUMVAL("789")
           MOVE WS-UINT TO WS-RESULT
           DISPLAY WS-RESULT

      * FUNCTION NUMVAL is a convenience function for basic number parsing
           COMPUTE WS-INT = FUNCTION NUMVAL("135")
           MOVE WS-INT TO WS-RESULT
           DISPLAY WS-RESULT

      * Error handling in COBOL is typically done with condition names
           COMPUTE WS-INT = FUNCTION NUMVAL("wat")
               ON SIZE ERROR
                   DISPLAY "Error: Invalid input"
           END-COMPUTE

           STOP RUN.

       CONVERT-HEX-TO-DEC.
      * This would be a custom routine to convert hexadecimal to decimal
      * Implementation details omitted for brevity
           EXIT.

In COBOL, number parsing is handled differently from Go:

  1. For floating-point numbers, we can use a simple MOVE statement to convert a string to a number.

  2. For integer parsing, we use the COMPUTE verb along with the FUNCTION NUMVAL, which converts a string to its numeric value.

  3. COBOL doesn’t have built-in support for hexadecimal parsing like Go’s ParseInt. We’d need to implement a custom routine for this (shown as CONVERT-HEX-TO-DEC in the example).

  4. There’s no direct equivalent to ParseUint in COBOL. Unsigned integers are typically handled using regular numeric fields.

  5. The FUNCTION NUMVAL serves a similar purpose to Go’s Atoi, converting a string to its numeric value.

  6. Error handling in COBOL is typically done using condition names like ON SIZE ERROR, rather than returning error values.

To run this COBOL program:

$ cobc -x number-parsing.cob
$ ./number-parsing
1.234
123
456
789
135
Error: Invalid input

Note that the exact output and behavior may vary depending on the COBOL compiler and runtime environment used.

Next, we’ll look at another common parsing task: URLs.