Skip to main content

[archive] Signed Integer/Decimal in key field

  • September 27, 2006
  • 3 replies
  • 0 views

[Migrated content. Thread originally posted on 26 September 2006]

Hi,

As a self-taught Cobol programmer, I occasionally come up against weird behaviours for which I cannot understand and find myself having to workaround.

Currently I'm trying to create a file where the keyfield is as follows:

03 S2-KEY.
05 S2-VAPC USAGE IS SIGNED-INT.
05 S2-BOM PIC X(10).

What I expect to happen is that when I read the file back, the most negative values appear at the start of the file, e.g. sequence would be -12, -5, 10 , 8. However, with the above the negative values appear after the positives in the sequence most to least negative. e.g. 8, 10, -12, -5

03 S2-KEY.
05 S2-VAPC PIC S999V999.
05 S2-BOM PIC X(10).

With the above it treats all S2-VAPC as alphanumeric, ignoring the sign, i.e. sequence can be -5, 8, 10, -12 etc.

Obviously I can workaround this issue by using the first key format and reading negative values before starting the file from the beginning to read the positive values.

What is the BEST way to acheive my desired result?

(Running AcuCobol 6.2 on SCO UNIX btw.)

3 replies

[Migrated content. Thread originally posted on 26 September 2006]

Hi,

As a self-taught Cobol programmer, I occasionally come up against weird behaviours for which I cannot understand and find myself having to workaround.

Currently I'm trying to create a file where the keyfield is as follows:

03 S2-KEY.
05 S2-VAPC USAGE IS SIGNED-INT.
05 S2-BOM PIC X(10).

What I expect to happen is that when I read the file back, the most negative values appear at the start of the file, e.g. sequence would be -12, -5, 10 , 8. However, with the above the negative values appear after the positives in the sequence most to least negative. e.g. 8, 10, -12, -5

03 S2-KEY.
05 S2-VAPC PIC S999V999.
05 S2-BOM PIC X(10).

With the above it treats all S2-VAPC as alphanumeric, ignoring the sign, i.e. sequence can be -5, 8, 10, -12 etc.

Obviously I can workaround this issue by using the first key format and reading negative values before starting the file from the beginning to read the positive values.

What is the BEST way to acheive my desired result?

(Running AcuCobol 6.2 on SCO UNIX btw.)
This has to do with the way the data is stored, (really??) and Acucorp personnel may have a method of storing the data so it works for you as you need it to.

Failing that, here's a suggestion that will work regardless of the PIC and USAGE clauses you define in the key, and the data storage compile options you specify:-03 S2-KEY.
    05 S2-NEG-OR-POS PIC X.
* This is the "sign".  N or P is stored here and puts
* "N"egative values at the start of the file, followed
* by "P"ositive values.
    05 S2-VAPC PIC 999V999. | No need to be signed
    05 S2-BOM PIC X(10).


Then, when writing to this file in code:-IF  your-number
    MOVE "N"  TO S2-NEG-OR-POS
    COMPUTE S2-VAPC = 999.999 your-number
* The smaller the negative value, the closer the key value will be to 999.999
* and so the file will store the "large" negative numbers at the start, and the
* "small" negatives at the end of the "N" section, near the positives.
ELSE
    MOVE "P"  TO S2-NEG-OR-POS
    COMPUTE S2-VAPC = your-number
END-IF.


When reading from the file:-IF  S2-NEG-OR-POS = "N"
    COMPUTE your-number = 999.999 - S2-VAPC
ELSE
    COMPUTE your-number = S2-VAPC
END-IF.


It's tacky, but it will work...

[Migrated content. Thread originally posted on 26 September 2006]

Hi,

As a self-taught Cobol programmer, I occasionally come up against weird behaviours for which I cannot understand and find myself having to workaround.

Currently I'm trying to create a file where the keyfield is as follows:

03 S2-KEY.
05 S2-VAPC USAGE IS SIGNED-INT.
05 S2-BOM PIC X(10).

What I expect to happen is that when I read the file back, the most negative values appear at the start of the file, e.g. sequence would be -12, -5, 10 , 8. However, with the above the negative values appear after the positives in the sequence most to least negative. e.g. 8, 10, -12, -5

03 S2-KEY.
05 S2-VAPC PIC S999V999.
05 S2-BOM PIC X(10).

With the above it treats all S2-VAPC as alphanumeric, ignoring the sign, i.e. sequence can be -5, 8, 10, -12 etc.

Obviously I can workaround this issue by using the first key format and reading negative values before starting the file from the beginning to read the positive values.

What is the BEST way to acheive my desired result?

(Running AcuCobol 6.2 on SCO UNIX btw.)
Cheers, I have affected a similar workaround already, but your method is slightly tidier. :)

[Migrated content. Thread originally posted on 26 September 2006]

Hi,

As a self-taught Cobol programmer, I occasionally come up against weird behaviours for which I cannot understand and find myself having to workaround.

Currently I'm trying to create a file where the keyfield is as follows:

03 S2-KEY.
05 S2-VAPC USAGE IS SIGNED-INT.
05 S2-BOM PIC X(10).

What I expect to happen is that when I read the file back, the most negative values appear at the start of the file, e.g. sequence would be -12, -5, 10 , 8. However, with the above the negative values appear after the positives in the sequence most to least negative. e.g. 8, 10, -12, -5

03 S2-KEY.
05 S2-VAPC PIC S999V999.
05 S2-BOM PIC X(10).

With the above it treats all S2-VAPC as alphanumeric, ignoring the sign, i.e. sequence can be -5, 8, 10, -12 etc.

Obviously I can workaround this issue by using the first key format and reading negative values before starting the file from the beginning to read the positive values.

What is the BEST way to acheive my desired result?

(Running AcuCobol 6.2 on SCO UNIX btw.)
That would be expected because the record key is specified at the 01 level, and groups are treated as PIC X() rather than each field individually.

The manual for RECORD KEY says:

The key of an indexed file may have any PICTURE and USAGE. Regardless of the PICTURE and USAGE specified, the key is always treated as an alphanumeric data item when the sort order of the file is determined (the individual bytes are compared with the collating sequence).

When you look at the acutal bytes in a PIC S9() field, the sign is encoded into the *last* digit (I think some old timers call this "overpunch"). For example, if the last digit of the value is a 1 and the number is positive, internally the last digit is stored as "A" (if it were a 2 it would a "B"). If it were a negative value, the last "1" digit would be stored as "J" (2 would be "K"). Since the record key group is treated as alphanumeric, the sign is encoded in the last digit and does not sort as one would expect.

Even the SIGN IS LEADING clause wouldn't help because positives are encoded as A-K and would sort before negatives. And the zero is encoded as "{" and "}", which throws it off even more.