View previous topic :: View next topic
|
Author |
Message |
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
Hi,
I have declared a variable as follows:
01 ws-var1 pic 9(8)
01 ws-var2 redefines ws-var1
02 ws-var3 pic s9(8) comp-3.
move 20070503 to ws-var1
what should be the value in ws-var3?
when i displayed the field, in hex it shows me
F F F F F F F F
2 F 0 F 0 F 7 F
variable 1 in hex is
F F F F F F F F
2 0 0 7 0 5 0 3
can you please explain me how is the value in variable 3 populated? |
|
Back to top |
|
|
acevedo
Active User
Joined: 11 May 2005 Posts: 344 Location: Spain
|
|
|
|
Quote: |
1.3.4.5 Packed-decimal (COMP-3) items
PACKED-DECIMAL and COMP-3 are synonyms on all platforms.
Packed-decimal items occupy 1 byte of storage for every two decimal digits you code in the PICTURE description, except that the rightmost byte contains only one digit and the sign. This format is most efficient when you code an odd number of digits in the PICTURE description, so that the leftmost byte is fully used. Packed-decimal items are handled as fixed-point numbers for arithmetic purposes.
|
|
|
Back to top |
|
|
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
Yeah, i understand that. but my query is, how is the value
F F F F F F F F
2 F 0 F 0 F 7 F
populated? According to me, the value should be
0 0 7 5 3
2 0 0 0 F |
|
Back to top |
|
|
acevedo
Active User
Joined: 11 May 2005 Posts: 344 Location: Spain
|
|
|
|
Code: |
F F F F F F F F
2 F 0 F 0 F 7 F
|
That's not what I see, instead
Code: |
--------
20070503
FFFFFFFF
20070503
|
anyway, I'd never use that redefines. Explain what you're trying to get.
by the way, '20070503', in it would be saved as
remember you shouldn't declare COMP-3 with even number. |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Vedam,
May I know why you are using redefines?
If you want to know the exact storage for a COMP-3 variable (date in this case) use following-
Code: |
01 comp-date 9(08) comp-3.
01 alpha-var x(05). |
move your date to date variable and display alpha variable.
do a hex on.. then you will see what you wanted to see. Let us know your results. |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Correction-
Quote: |
01 comp-date 9(08) comp-3.
01 alpha-var redefines comp-date x(05). |
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
The situation as posted should never be coded. It is another of the things that the compiler might let you do, but will serve no good purpose and should be avoided.
Code: |
01 ws-var1 pic 9(8)
01 ws-var2 redefines ws-var1
02 ws-var3 pic s9(8) comp-3.
|
This is an unacceptable set of code - it has multiple problems. Rather than discuss what all might happen (which will get many conflicting opinions), focus might be better spent on what the code needs to actually do.
Please post what you are trying to accomplich and we can offer suggestions. |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Dick,
I think Vedam wants to see how date is stored in COMP-3, my guess is he got confused by assuming ( 01 comp-date 9(08) comp-3 )will take 8 bytes. So he used redefinition and tried to print the contents.
That's why I suggested him to do-
01 comp-date 9(08) comp-3.
01 alpha-var redefines comp-date x(05).
Let's see if he clarifies more on his question. |
|
Back to top |
|
|
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
hi,
I have a date with pic 9(8). i have to write the date to a file. I thought I could use 9(8) comp-3 which will be more efficient. Instead of moving from var-1 to var-3, i used redefines clause which was my mistake.
thanks all for the clarification.
How will 'move' of a non comp variable to a comp variable work fine? as for an 8 byte, comp-3 will store the value in a 5 byte. when this value is being moved to another variable of 8 bytes, it should throw error right?? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
If your "non comp" field has valid numeric data you will have no problem.
Due to the way comp-3 is stored it is a good idea to use an odd number of digits when defining comp-3 field(s). 9(8) comp-3 is the same size as 9(9) comp-3 (5 bytes).
This
Quote: |
as for an 8 byte, comp-3 will store the value in a 5 byte. when this value is being moved to another variable of 8 bytes, it should throw error right |
might be worded differently. I'm not sure why the comp-3 field would be moved to an 8-byte variable. I may be misunderstanding something. . .
You can move decimal numbers to packed-decimal (comp-3). You can move comp-3 data to binary, zoned, or edited fields with no problems. You should not move comp-3 fields to fields that are not defned as some kind of numeric. The compiler will often let you compile code that will cause problems. |
|
Back to top |
|
|
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
the reason why would i move comp-3 variable to an 8 byte variable is as follows:
I have a database record which has a date field in it defined as 9(8) COMP-3. I have to process the field in such a way that I require year, month and day separately.
So, I will move the 9(8) comp-3 variable to a 9(8) variable. I'll redefine this 9(8) variable.
I was wondering how a packed field when moved to a non comp field will fetch values correctly. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
First - i believe i misunderstood what 8-byte meant - i thought it meant x(8).
If you move 9(8) comp-3 to 9(8) you should have no problem. And then you can redefine the 9(8) as whatever you need.
Let us know if there is anything that needs clarification. |
|
Back to top |
|
|
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
Normally an 8 byte COMP-3 variable occupies 5 bytes. When its value is "move"d to a non-comp variable of 8 bytes, does it get unpacked? i.e., the 5 bytes get unpacked to 8 bytes and fit in the non-comp variable?
here "move" means the statement "move var-1 to var-2" |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Quote: |
Normally(always) an 8 byte (wrong, you mean digit)COMP-3 variable occupies 5 bytes. When its value is "move"d to a non-comp variable of 8 bytes, does it get unpacked? i.e., the 5 bytes get unpacked to 8 bytes and fit in the non-comp variable(You mean ZD then) ? YES |
|
|
Back to top |
|
|
Vedam
New User
Joined: 24 Apr 2007 Posts: 23 Location: India
|
|
|
|
yeah..it is not byte..it is digit..by non-comp, i mean a variable having PIC 9(8). |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
If you move the 9(8) comp-3 field to a 9(8) field, you will get the result you want. |
|
Back to top |
|
|
|