I need to move a value eg 10 which is in a field pic x(300) to a s9(7) comp field. Do I need to do some kind of inspect, unstring or something like that ? It seemd that a plain old move x-value to 9-value won't work. Help please ?
first you have to isolate the numeric parts out of the pic x(300) field, so if you know where the numeric parts are, it will help. If you know the exact place, you can redefine that part with a pic 9(..). If you don't know the place, you can use all kinds of ways to get it, inspect is one possibility, a perform varying and using reference modification is a possibility.
If you found it, you can move it to an other alfanumeric field which has a numeric redefines. So for example, if you found out that the numeric part starts at START-POS and the length is LENGTH-NUM you can move it to a field like:
01 STRING-NUM PIC X(18).
01 NUM REDEFINES STRING-NUM PIC 9(18).
MOVE ZERO TO NUM
MOVE STRING-X-300(START-POS:LENGTH-NUM) TO
STRING-NUM (19 - LENGTH-NUM:LENGTH-NUM)
and now NUM contains your numeric data.
A funny but not most efficient way to find the first part of the string is by defining this:
The answer by Crox is good, but didn't mention how to find the start of the string. Try this:
05 STRING-WITH-NUMBER PIC X(300).
05 STR-POS PIC 999 VALUE 1.
05 STR-LEN PIC 99 VALUE ZEROS.
05 RESULT-NO PIC 9(18) VALUE ZEROS.
05 RESULT-NO-STR REDEFINES RESULT-NO PIC X(18) JUST.
MOVE 1 TO STR-POS.
INSPECT STRING-WITH-NUMBER TALLYING STR-POS FOR LEADING SPACES.
INSPECT STRING-WITH-NUMBER TALLYING STR-LEN (STR-POS TALLYING STR-LEN
FOR CHARACTERS BEFORE SPACES.
INITITALIZE RESULT-NO.
IF STR-LEN <= 18 AND STRING-WITH-NUMBER (STR-POS: STR-LEN) NUMERIC
MOVE STRING-WITH-NUMBER (STR-POS: STR-LEN) TO RESULT-NO-STR
MOVE RESULT-NO TO ... (wherever you want this, can be comp field)
END-IF.
If there is any possibility of a non-integer number (one with decimal positions), I can give you the additional code, just let me know.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.