Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MF COBOL - Convert index value to numeric

Status
Not open for further replies.

RICHINMINN

Programmer
Dec 31, 2001
138
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.)
 
Cant you just move the index name to a standard numeric
variable?
 
You can't MOVE an INDEX, but you can use the SET statement. Define a numeric field (it can be zoned decimal or packed). Use the SET statement this way:

SET num-field TO index-field

This will convert the index value to a numeric value.
 
You might also run a timing test using reference modification without using an array . . .
 
I alway use reference modification (substring notation) to do this sort of thing.
 
Are you trying to find the length of a string? If so, would this be easier?
Code:
      *SCAN******************************************************** 
      *  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.
 
Please re-read the original request. The "calculate-keystrokes" routine only does part of the requirement.
 
weberm, your code assumes no leading spaces. You must also inspect the unreversed WS-STRING for leading spaces. This is a common analysis error by beginning programmers.
 
Here's the code that I ended up using: (WORK-FIELD is used for inspecting a number of different-length fields, ranging from 20 to 300 bytes. The length of field to inspect is contained in MAX-LEN. If, for example, MAX-LEN has a value of 20, only the first 20 bytes of WORK-FIELD need to be inspected for the actual string length. The string can have leading and/or trailing spaces, which need to be removed, and the length of the remaining string calculated (WORK-LENGTH).

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.)
 
How about something like the following?

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top