## MF COBOL - Convert index value to numeric

## MF COBOL - Convert index value to numeric

(OP)

IBM Mainframe COBOL:

I've got a 300-character work area where I insert an alphanumeric string and then determine the beginning and ending non-space characters of the string so I can determine the number of characters in the string. I've got the 300-character work area indexed to speed the lookup of the first non-space character and the final non-space character, but once I've got the beginning and ending index values, how can I determine the overall length of the non-space portion of the character string? I can't use the index values to perform the math needed.

In other words, how do I convert an index value into a "normal" numeric value?

Rich (in Minn.)

I've got a 300-character work area where I insert an alphanumeric string and then determine the beginning and ending non-space characters of the string so I can determine the number of characters in the string. I've got the 300-character work area indexed to speed the lookup of the first non-space character and the final non-space character, but once I've got the beginning and ending index values, how can I determine the overall length of the non-space portion of the character string? I can't use the index values to perform the math needed.

In other words, how do I convert an index value into a "normal" numeric value?

Rich (in Minn.)

## RE: MF COBOL - Convert index value to numeric

variable?

## RE: MF COBOL - Convert index value to numeric

SET num-field TO index-field

This will convert the index value to a numeric value.

## RE: MF COBOL - Convert index value to numeric

## RE: MF COBOL - Convert index value to numeric

## RE: MF COBOL - Convert index value to numeric

## CODE

* CALCULATE THE NUMBER OF KEY STROKES FOR A GIVEN STRING. *

* WS-FIELD-LENGTH: SIZE OF WS-STRING *

* WS-LENGTH: NUMBER OF KEYSTROKES IN WS-STRING *

* WS-SPACES: NUMBER OF TRAILING SPACES *

* WS-STRING: STRING TO BE TESTED *

*************************************************************

CALCULATE-KEYSTROKES.

INITIALIZE WS-SPACES, WS-LENGTH.

INSPECT FUNCTION REVERSE(WS-STRING)

TALLYING WS-SPACES FOR LEADING SPACES.

COMPUTE WS-LENGTH = WS-FIELD-LENGTH - WS-SPACES.

## RE: MF COBOL - Convert index value to numeric

## RE: MF COBOL - Convert index value to numeric

## RE: MF COBOL - Convert index value to numeric

IF WORK-FIELD(1:MAX-LEN) NOT = SPACES

IF WORK-FIELD(MAX-LEN:1) = SPACE

*** AT LEAST 1 TRAILING SPACE SO GET COUNT

INSPECT FUNCTION REVERSE(WORK-FIELD(1:MAX-LEN))

TALLYING END-SPACE-COUNT

FOR LEADING SPACES

ELSE

MOVE 0 TO END-SPACE-COUNT

END-IF

IF WORK-FIELD(1:1) = SPACE

*** AT LEAST 1 LEADING SPACE SO GET COUNT

INSPECT WORK-FIELD(1:MAX-LEN)

TALLYING BEG-SPACE-COUNT

FOR LEADING SPACES

ELSE

MOVE 0 TO BEG-SPACE-COUNT

END-IF

COMPUTE WORK-LENGTH

= MAX-LEN - (BEG-SPACE-COUNT + END-SPACE-COUNT)

ELSE

MOVE 0 TO BEG-SPACE-COUNT

END-SPACE-COUNT

WORK-LENGTH.

MOVE WORK-FIELD(BEG-SPACE-COUNT + 1,WORK-LENGTH)

TO OUTPUT-FIELD.

Rich (in Minn.)

## RE: MF COBOL - Convert index value to numeric

It doesn't look like you really need to know how long the field is because when you move a shorter field to a longer one it pads the receiving field with spaces. How about if we first left justify the WORK-FIELD to get rid of leading spaces? Doesn't really matter how many leading spaces there are if we get rid of them.

Then move that field for the maximum possible length (MAX-LEN) to the output.

IF WORK-FIELD(1:MAX-LEN) NOT = SPACES

PERFORM

UNTIL WORK-FIELD(1:1)NOT = SPACE

MOVE WORK-FIELD (2: MAX-LEN - 1)

TO WORK-FIELD (1:MAX-LEN)

END-PERFORM

END-IF

MOVE WORK-FIELD(1:MAX-LEN)

TO OUTPUT-FIELD.