Reputation: 13
I have been stuck on this issue for a while now. Help would be much appreciated.
I have a 4 digit number read in from a file and I need to take the inside 2 digits. I thought that reading the number in as a string would be a good idea, then take the the middle two digits in a substring and use the numval function to convert them back. Unfortunately, I can't figure out how to obtain the inside two characters.
Ex. I have the number 5465, I want to get 46.
Upvotes: 1
Views: 14575
Reputation: 10765
What Keith Thompson proposes will work fine. You might want to check that I > 0 and J > 0. That wouldn't be a problem if you know 1000 <= I <= 9999 always.
IBM Enterprise COBOL includes a MOD function, which may or may not be available with your compiler.
I think you could also do the following...
01 A-GROUP.
05 A-NUMBER PIC 9999 VALUE ZEROES.
05 A-STRING REDEFINES A-NUMBER.
10 FILLER PIC X.
10 THE-MIDDLE-TWO-DIGITS PIC XX.
10 FILLER PIC X.
MOVE your-number TO A-NUMBER.
This should work whether your-number is defined as COMP or COMP-3, provided 0 <= your-number <= 9999.
Upvotes: 5
Reputation: 13076
01 a-long-piece-of-data.
05 the-first-character pic x.
05 the-two-characters-we-want pic xx.
05 the-last-character pic x.
01 a-short-piece-of-data pic xx.
01 filler redefines a-short-piece-of-data.
05 a-short-unsigned-number pic 99.
MOVE the-two-characters-we-want TO a-short-piece-of-data
MOVE/ADD/COMPUTE/whatever a-short-unsigned-number
or MOVE a-short-piece-of-data TO wherever
Have a signed number and want to retain the sign?
01 a-long-number PIC S9(4).
01 FILLER REDEFINES a-long-number.
05 FILLER PIC X.
05 an-integer-with-one-decimal-place PIC S99V9.
01 a-short-number-no-decimals PIC S99.
MOVE an-integer-with-one-decimal-place TO a-short-number-no-decimals
Upvotes: 0
Reputation: 4263
You can use reference modification. Consider the following:
1 WS-MY-FIELD Pic X(4).
1 WS-TGT-FIELD Pic X(2).
...
Move WS-MY-FIELD (2:2) to WS-TGT-FIELD
The first number indicates the start position (1 based) and the second number indicates the length.
Upvotes: 5
Reputation: 263307
If you have it as a number rather than as a string, you can do it arithmetically. Given that I
is 5465
, and you want to store 46
in J
:
DIVIDE I BY 10 GIVING J.
DIVIDE J BY 100 GIVING ignored REMAINDER J.
Upvotes: 3