View previous topic :: View next topic
|
Author |
Message |
amarjit singh
New User
Joined: 08 Mar 2013 Posts: 30 Location: india
|
|
|
|
hello,
my cobol program is getting data of X(02) from input file with leading spaces or trailing space.
example:
-----------
data can be ' 1' or '1 '.
as per the requirement, we need to convert into valid numric data (exm '01' ) for further proceesing.
my approach:
Code: |
IF A1:1) = ' '
MOVE '0' TO A(1:1)
ELSE
IF A(2:1) = ' '
MOVE A(1:1) TO B
MOVE '0' TO A(1:1)
MOVE B TO A(2:1)
END IF.
|
Please suggest a better way, if the above is not appropiate.
thanks.
Code'd |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Click on the IBM Manuals link at the top of this page, find the COBOL Language Reference manual, and read up on the FUNCTION NUMVAL in 7.1.32. |
|
Back to top |
|
|
amarjit singh
New User
Joined: 08 Mar 2013 Posts: 30 Location: india
|
|
|
|
thanks Robert. its working fine for numeric data.
but i am getting error when i have space /any alphabet char in the input field.
any suggestion for that. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
I do not understand your post. NUMVAL explicitly handles leading or trailing spaces, so spaces should not cause you any problems. One way to handle non-numeric data would be to define a 2-byte variable C and use this code (where B is defined as PIC 9, not PIC X):
Code: |
MOVE A TO C
INSPECT C
REPLACING ALL SPACES BY ZEROES
IF C NUMERIC
COMPUTE B = FUNCTION NUMVAL (A)
ELSE
<handle non-numeric A>
END-IF. |
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Code: |
01 a-nice-name.
05 ann-first-byte PIC X.
88 ann-first-byte-is-space VALUE SPACE.
05 ann-first-byte-num
REDEFINES ann-first-byte PIC 9.
05 ann-second-byte PIC X.
88 ann-second-byte-is-space VALUE SPACE.
05 ann-second-byte-num
REDEFINES ann-second-byte PIC 9.
01 a-nice-name-num REDEFINES a-nice-name PIC 99.
|
Code: |
MOVE input-data TO a-nice-name
EVALUATE TRUE
WHEN a-nice-name NUMERIC
PERFORM two-digit-number
WHEN ( ann-first-byte-is-space
AND ann-second-byte NUMERIC )
PERFORM one-numeric-leading-space
WHEN ( ann-second-byte-is-space
AND ann-first-byte NUMERIC )
PERFORM one-numeric-trailing-space
WHEN OTHER
PERFORM not-numeric-enough
END-EVALUATE |
In PERFORMed paragraphs/SECTIONs, use the appropriate "-num" fields. |
|
Back to top |
|
|
amarjit singh
New User
Joined: 08 Mar 2013 Posts: 30 Location: india
|
|
|
|
hello Robert when i have spaces in input file NUMVAL is giving error AB U1097.
any way thank you Robert and Bill for the valuable time .. i use both the logic and its working fine for me. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
What does "AB" mean in "AB U1097"? Did you mean "abend"?
OTOH, the description of U1097 says:
Quote: |
Message Format: IGZ097I THE CALLER WAS NOT A COBOL PROGRAM.
Description: A non-COBOL program attempted to call a VS COBOL II program without going through the appropriate interface and, as a result, VS COBOL II could not find a COBOL environment. The job step is cancelled.
User Action: See VS COBOL II APPLICATION PROGRAMMING GUIDE for an explanation of CALL statement rules and requirements. |
That makes me curious.
It'd be nice if you can, please, share what finally worked for you. |
|
Back to top |
|
|
amarjit singh
New User
Joined: 08 Mar 2013 Posts: 30 Location: india
|
|
|
|
Hi anuj,
below code works for me:
input file structure:
-----------------------
Code: |
01 WS-IN-RECORD.
05 WS1.
10 WS1-FIRST-BYTE PIC X.
88 WS1-FIRST-BYTE-IS-SPACE VALUE SPACE.
10 WS1-FIRST-BYTE-NUM
REDEFINES WS1-FIRST-BYTE PIC 9.
10 WS1-SECOND-BYTE PIC X.
88 WS1-SECOND-BYTE-IS-SPACE VALUE SPACE.
10 WS1-SECOND-BYTE-NUM
REDEFINES WS1-SECOND-BYTE PIC 9.
05 FILLER PIC X(78) VALUE SPACES.
WS2 PIC99. |
procedure division:
----------------------
Code: |
IF WS1 IS NUMERIC THEN
NEXT SENTENCE
ELSE
IF ( WS1-FIRST-BYTE-IS-SPACE
AND WS1-SECOND-BYTE IS NUMERIC ) THEN
COMPUTE WS2 = FUNCTION NUMVAL(WS1)
MOVE WS2 TO WS1
ELSE
IF ( WS1-SECOND-BYTE-IS-SPACE
AND WS1-FIRST-BYTE IS NUMERIC ) THEN
COMPUTE WS2 = FUNCTION NUMVAL(WS1)
MOVE WS2 TO WS1
ELSE
PERFORM ERROR-DATA
END-IF. |
I was getting abend initialy, when there was space in input WS1.
and in NUMVAL i was getting U1097.
thanks. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Amarjit - I've edited your code to add BBcode Tags, please learn to use them. "WS2 PIC99" was just lumping around with no 'label' attached to it, so I'm not sure if that's way you wanted it. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Why, when you've identified one byte as being numeric and you have a field ready for it which is defined as PIC 9, do you use NUMVAL rather than a plain, simple, must less stressful to the CPU, MOVE? |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
I don't understand this move:
- WS2 is of PIC 9(2) while WS1 is PIC X(80).
As Robert has also said - The NUMVAL function returns the numeric value represented by the alphanumeric character string or national character string specified as the argument. The function removes any leading or trailing spaces in the string to produce a numeric value. And 'space can be a string of one or more spaces.' So the mystry about U1097 still remains. I'll try to run some more tests to see if I can recreated this at my end. |
|
Back to top |
|
|
amarjit singh
New User
Joined: 08 Mar 2013 Posts: 30 Location: india
|
|
|
|
HI,
WS1 is of pic X(02) followed by filler x(78).
WS2 is also independent variable.
thanks. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
- I overlookd your Data-definition. That's where BBcode helps. |
|
Back to top |
|
|
|