View previous topic :: View next topic |
Author |
Message |
shashank.sharma Beginner
Joined: 18 Apr 2007 Posts: 16 Topics: 10
|
Posted: Wed Oct 10, 2007 10:09 am Post subject: Numeric to Alphanumeric move |
|
|
Hi,
I have:
01 A-1 PIC S9(05)V99 VALUE 78.89.
01 A-3 PIC X(09).
I want A-3 to contain '78.80 ' using minimum no. of COBOL statements. How can I do it? |
|
Back to top |
|
|
CraigG Intermediate
Joined: 02 May 2007 Posts: 202 Topics: 0 Location: Viginia, USA
|
Posted: Wed Oct 10, 2007 10:58 am Post subject: Re: Numeric to Alphanumeric move |
|
|
shashank.sharma wrote: | Hi,
I have:
01 A-1 PIC S9(05)V99 VALUE 78.89.
01 A-3 PIC X(09).
I want A-3 to contain '78.80 ' using minimum no. of COBOL statements. How can I do it? |
Code: | 01 a-3 pic -----9.99.
move a-1 to a-3. |
That is so basic I know you didn't bother looking in the manual. |
|
Back to top |
|
|
vivek1983 Intermediate
Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Thu Oct 11, 2007 2:17 am Post subject: |
|
|
shashank.sharma,
Quote: |
want A-3 to contain '78.80 '
|
Do you want to round off the last digit? Meaning - 78.89 becomes 78.80? or 78.90? Or was it a typo? _________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
|
shashank.sharma Beginner
Joined: 18 Apr 2007 Posts: 16 Topics: 10
|
Posted: Thu Oct 11, 2007 5:33 am Post subject: |
|
|
By doing this A-3 will have ' 78.89'. But I want it to be as '78.89 ' |
|
Back to top |
|
|
CraigG Intermediate
Joined: 02 May 2007 Posts: 202 Topics: 0 Location: Viginia, USA
|
Posted: Thu Oct 11, 2007 7:01 am Post subject: |
|
|
shashank.sharma wrote: | By doing this A-3 will have ' 78.89'. But I want it to be as '78.89 ' |
Because of a your lack of formatting in your request I assumed that you wanted it left justified as is normal with numbers. |
|
Back to top |
|
|
vivek1983 Intermediate
Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Thu Oct 11, 2007 8:00 am Post subject: |
|
|
shashank.sharma,
Quote: |
By doing this A-3 will have ' 78.89'. But I want it to be as '78.89 '
|
My suggestion:
1. MOVE A-1 TO A-3.
2. Now A-3 will have '78.89'.
3. Use inspect function on A-3 and find the position of '.' and add 1 to this field. (say a4. This will contain the position where '.' is)
4. now move A-3(1:A4) to A5.
5. Compute A-6 = A5 * 10.
6. Move A-6 to A-3. A-3 will have 78.80.
There may be some other easy ways also. But cannot think of those right now.! _________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
|
CICS Guy Intermediate
Joined: 30 Apr 2007 Posts: 292 Topics: 3
|
Posted: Thu Oct 11, 2007 8:07 am Post subject: Re: Numeric to Alphanumeric move |
|
|
shashank.sharma wrote: | I want A-3 to contain using minimum no. of COBOL statements. |
Code: | 01 A-1 PIC S9(05)V99 VALUE 78.89.
01 A-2 PIC -----9.99.
01 A-3 PIC X(09).
MOVE A-1 TO A-2.
UNSTRING A-2 DELIMITED BY ALL SPACES INTO A-3. |
Without testing, you might need a second A-3..... |
|
Back to top |
|
|
vivek1983 Intermediate
Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Thu Oct 11, 2007 8:36 am Post subject: |
|
|
shashank.sharma,
Please disregard my previous suggestion.
I think the below suggestion should work fine:
Code: |
01 A-1 PIC S9(05)V99 VALUE 78.89.
01 A-2 PIC S9(05)V9.
01 A-4 PIC S9(05)V99.
01 A-3 PIC X(09).
MOVE A-1 TO A-2
MOVE A-2 TO A-4
MOVE A-4 TO A-3
|
A-4 will have '78.80' now. _________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Oct 11, 2007 10:09 am Post subject: |
|
|
Vivek
You cannot move a numeric with decimal to an alphanumeric. Compile error.
CICS Guy
Your solution is really close, but there's 2 things wrong. First the compiler gives an error when you try to use the numeric edit field in the UNSTRING:
Code: |
IGYPA3105-S IDENTIFIER "A-2 (NUMERIC-EDITED)" WAS NEITHER AN ALPHANUMERIC, DBCS, OR NATIONAL DATA ITEM NOR AN ALPHANUMERIC OR NATIONAL FUNCTION. THE STATEMENT WAS DISCARDED.
|
That's easily corrected by redefining the numeric edit field. The second issue is that UNSTRING doesn't move anything to the A-3 field the way you have it coded. You need to add one more work field. You'd alwo want to initialize the receiving field because UNSTRING does not space fill. Here's how I got this to work:
Code: |
05 A-1 PIC S9(05)V99 VALUE 78.89.
05 A-2 PIC ZZZZZZ.99.
05 A-2-X REDEFINES A-2
PIC X(09).
05 A-3 PIC X(09).
05 A-4 PIC X(09).
DISPLAY A-1
MOVE A-1 TO A-2
MOVE SPACES TO A-3
UNSTRING A-2-X DELIMITED BY ALL SPACES INTO A-4 A-3
DISPLAY 'A-3 AFTER STRING: ''' A-3 ''''
DISPLAY 'A-4 AFTER STRING: ''' A-4 ''''
.
<from sysout>
000788I
A-3 AFTER STRING: '78.89 '
A-4 AFTER STRING: ' '
|
There is a way to do this without using any intermediate fields just by redefining one of them. Here goes:
Code: |
05 A-1 PIC S9(05)V99 VALUE 78.89.
05 A-3 PIC X(09).
05 A-3-E REDEFINES A-3
PIC ZZZZZZ.99.
DISPLAY A-1
MOVE A-1 TO A-3-E
DISPLAY 'A-3 BEFORE PERFORM: ''' A-3 ''''
PERFORM VARYING PTR1 FROM 1 BY 1
UNTIL A-3(PTR1:1) > '0'
OR A-3(PTR1:1) = '.'
END-PERFORM
DISPLAY 'PTR1 AFTER PERFORM: ' PTR1
MOVE A-3(PTR1:) TO A-3
DISPLAY 'A-3 AFTER STRING: ''' A-3 ''''
<from sysout>
000788I
A-3 BEFORE PERFORM: ' 78.89'
PTR1 AFTER PERFORM: 00005
A-3 AFTER STRING: '78.89 '
|
Both ways use 3 COBOL statements (MOVE,MOVE,UNSTRING or MOVE,PERFORM,MOVE). Not knowing all the rules for the homework assignment, I don't know which one would get the most credit |
|
Back to top |
|
|
CICS Guy Intermediate
Joined: 30 Apr 2007 Posts: 292 Topics: 3
|
Posted: Thu Oct 11, 2007 10:58 am Post subject: Re: Numeric to Alphanumeric move |
|
|
jsharon1248 wrote: | Your solution is really close, but there's 2 things wrong. |
shashank.sharma wrote: | I want A-3 to contain using minimum no. of COBOL statements. |
Code: | 01 A-1 PIC S9(05)V99 VALUE 78.89.
01 A-X.
05 A-2 PIC -----9.99.
01 A-3 PIC X(09).
MOVE A-1 TO A-2.
UNSTRING A-X DELIMITED BY ALL SPACES INTO A-3 A-3. |
Thanks for the test, without testing, I wasn't sure if the initial delimiters would be skipped or not.
My manual shows that identifier-1 must be an alphanumeric, alpahnumeric-edited, alphabetic, national, or DBCS data item but I added the group level for you. |
|
Back to top |
|
|
|
|