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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alphanumeric to numeric comp

Status
Not open for further replies.

gentle

Technical User
Oct 19, 2002
1
JM
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 ?
 
Hi,

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:

01 STRING-X-300.
03 STRING-X-300-FIRST-BYTE PIC X.
03 STRING-X-300-REST PIC X(299).

IF STRING-X-300 NOT = SPACE
PERFORM UNTIL STRING-X-300-FIRST-BYTE IS NUMERIC
MOVE STRING-X-300-REST TO STRING-X-300
END-PERFORM
END-IF

Have fun! :)

 
Oh yeah, I forgot, if you finally found the numeric stuff, you can do a numeric move to the comp field of course!
 
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.

Betty Scherber
Brainbench MVP for COBOL II
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top