As an exercise, I am trying to write routine, that should accept Textual-Data as Input, and return a Number-Representation as its Output. It should work just like the Numeric Intrinsic Function NUMVAL.
I have written the following sub-routine in COBOL and tested it. I just check every character, if it falls between '0' THRU '9', I move it to the Output Numeric Field. Could anyone give me some inputs, about which other possibilities need to be taken care of? Or is this comprehensive.
IDENTIFICATION DIVISION. 00010000
PROGRAM-ID. PROG99. 00020000
*---------------------------------------------------------------* 00030000
* PROGRAM-ID : PROG99 DATE : 09/06/11 * 00030100
* Author : Quasar Chunawala * 00030200
* Description: This program accepts alphanumeric Text Input, * 00030300
* performs Validation, and returns back a numeric * 00030400
* representation of it. * 00030500
*---------------------------------------------------------------* 00031000
ENVIRONMENT DIVISION. 00040000
00050000
DATA DIVISION. 00060000
WORKING-STORAGE SECTION. 00070000
*---------------------------------------------------------------* 00070100
* Literals and Constants * 00070201
*---------------------------------------------------------------* 00070300
01 WS-LIT-MAX-DIGITS PIC 99 VALUE 18. 00070401
01 WS-LIT-ONE PIC 99 VALUE 01. 00070501
00070601
*---------------------------------------------------------------* 00070701
* Flags and Switches * 00070801
*---------------------------------------------------------------* 00070901
01 FLAGS-AND-SWITCHES. 00071000
05 WS-DEBUG-SW PIC X VALUE 'Y'. 00072000
88 DEBUG-ON VALUE 'Y'. 00073000
88 DEBUG-OFF VALUE 'N'. 00074000
05 END-OF-STRING-SW PIC X VALUE 'N'. 00074100
88 END-OF-STRING VALUE 'Y'. 00074200
00075000
*---------------------------------------------------------------* 00076000
* Counters * 00077000
*---------------------------------------------------------------* 00078000
01 WS-COUNTERS. 00079000
05 WS-IPOS PIC S9(04) COMP-3 00079100
VALUE ZEROES. 00079200
05 WS-OPOS PIC S9(04) COMP-3 00079300
VALUE ZEROES. 00079400
*---------------------------------------------------------------* 00079500
* Miscallaneous * 00079600
*---------------------------------------------------------------* 00079700
01 WS-CHARACTER PIC X. 00079800
01 WS-PARA-NAME PIC X(30). 00079900
00080000
LINKAGE SECTION. 00081000
01 LS-ALPHANUM-INPUT PIC X(18). 00090000
01 LS-RETURN-CODE PIC 99. 00100000
01 LS-NUM-OUTPUT PIC S9(18). 00110000
00120000
PROCEDURE DIVISION USING LS-ALPHANUM-INPUT 00130000
LS-RETURN-CODE 00131000
LS-NUM-OUTPUT. 00132000
00133000
0000-MAIN. 00140000
MOVE '0000-MAIN' TO WS-PARA-NAME 00150000
PERFORM 8000-DEBUG 00160000
00170000
PERFORM 1000-PARSE-STRING 00171000
THRU 1000-PARSE-STRING 00172000
UNTIL END-OF-STRING 00173000
00174000
MOVE 00 TO LS-RETURN-CODE 00175000
00176000
PERFORM 9999-GOBACK. 00180000
00190000
1000-PARSE-STRING. 00191000
MOVE '1000-PARSE-STRING' TO WS-PARA-NAME 00191102
PERFORM 8000-DEBUG 00191200
00191300
ADD WS-LIT-ONE TO WS-IPOS 00191401
MOVE LS-ALPHANUM-INPUT(WS-IPOS:1) TO WS-CHARACTER 00191500
EVALUATE WS-CHARACTER 00191600
WHEN '0' THRU '9' 00191700
ADD WS-LIT-ONE TO WS-OPOS 00191801
MOVE WS-CHARACTER TO LS-NUM-OUTPUT(WS-OPOS:1) 00191900
END-EVALUATE 00192001
00192101
IF WS-IPOS = WS-LIT-MAX-DIGITS 00192201
SET END-OF-STRING TO TRUE 00192301
END-IF. 00192401
1000-PARSE-STRING-EXIT. EXIT. 00192500
00193000
8000-DEBUG. 00194000
IF DEBUG-ON 00195000
DISPLAY WS-PARA-NAME 00196000
END-IF. 00197000
00198000
9999-GOBACK. 00200000
GOBACK. 00210000
PROGRAM-ID. PROG99. 00020000
*---------------------------------------------------------------* 00030000
* PROGRAM-ID : PROG99 DATE : 09/06/11 * 00030100
* Author : Quasar Chunawala * 00030200
* Description: This program accepts alphanumeric Text Input, * 00030300
* performs Validation, and returns back a numeric * 00030400
* representation of it. * 00030500
*---------------------------------------------------------------* 00031000
ENVIRONMENT DIVISION. 00040000
00050000
DATA DIVISION. 00060000
WORKING-STORAGE SECTION. 00070000
*---------------------------------------------------------------* 00070100
* Literals and Constants * 00070201
*---------------------------------------------------------------* 00070300
01 WS-LIT-MAX-DIGITS PIC 99 VALUE 18. 00070401
01 WS-LIT-ONE PIC 99 VALUE 01. 00070501
00070601
*---------------------------------------------------------------* 00070701
* Flags and Switches * 00070801
*---------------------------------------------------------------* 00070901
01 FLAGS-AND-SWITCHES. 00071000
05 WS-DEBUG-SW PIC X VALUE 'Y'. 00072000
88 DEBUG-ON VALUE 'Y'. 00073000
88 DEBUG-OFF VALUE 'N'. 00074000
05 END-OF-STRING-SW PIC X VALUE 'N'. 00074100
88 END-OF-STRING VALUE 'Y'. 00074200
00075000
*---------------------------------------------------------------* 00076000
* Counters * 00077000
*---------------------------------------------------------------* 00078000
01 WS-COUNTERS. 00079000
05 WS-IPOS PIC S9(04) COMP-3 00079100
VALUE ZEROES. 00079200
05 WS-OPOS PIC S9(04) COMP-3 00079300
VALUE ZEROES. 00079400
*---------------------------------------------------------------* 00079500
* Miscallaneous * 00079600
*---------------------------------------------------------------* 00079700
01 WS-CHARACTER PIC X. 00079800
01 WS-PARA-NAME PIC X(30). 00079900
00080000
LINKAGE SECTION. 00081000
01 LS-ALPHANUM-INPUT PIC X(18). 00090000
01 LS-RETURN-CODE PIC 99. 00100000
01 LS-NUM-OUTPUT PIC S9(18). 00110000
00120000
PROCEDURE DIVISION USING LS-ALPHANUM-INPUT 00130000
LS-RETURN-CODE 00131000
LS-NUM-OUTPUT. 00132000
00133000
0000-MAIN. 00140000
MOVE '0000-MAIN' TO WS-PARA-NAME 00150000
PERFORM 8000-DEBUG 00160000
00170000
PERFORM 1000-PARSE-STRING 00171000
THRU 1000-PARSE-STRING 00172000
UNTIL END-OF-STRING 00173000
00174000
MOVE 00 TO LS-RETURN-CODE 00175000
00176000
PERFORM 9999-GOBACK. 00180000
00190000
1000-PARSE-STRING. 00191000
MOVE '1000-PARSE-STRING' TO WS-PARA-NAME 00191102
PERFORM 8000-DEBUG 00191200
00191300
ADD WS-LIT-ONE TO WS-IPOS 00191401
MOVE LS-ALPHANUM-INPUT(WS-IPOS:1) TO WS-CHARACTER 00191500
EVALUATE WS-CHARACTER 00191600
WHEN '0' THRU '9' 00191700
ADD WS-LIT-ONE TO WS-OPOS 00191801
MOVE WS-CHARACTER TO LS-NUM-OUTPUT(WS-OPOS:1) 00191900
END-EVALUATE 00192001
00192101
IF WS-IPOS = WS-LIT-MAX-DIGITS 00192201
SET END-OF-STRING TO TRUE 00192301
END-IF. 00192401
1000-PARSE-STRING-EXIT. EXIT. 00192500
00193000
8000-DEBUG. 00194000
IF DEBUG-ON 00195000
DISPLAY WS-PARA-NAME 00196000
END-IF. 00197000
00198000
9999-GOBACK. 00200000
GOBACK. 00210000
Thank you very much.