I AM FROM THE OLD MAINFRAME WORLD WHERE NUMERIC FILEDS
WERE NEAT RIGHT JUSTIFIED ZERO FILLED FIELDS.
NOW THEY ARE LEFT JUSTIFIED WITH SPACES AFTER THE NUMBER.
HOW DO I WORK WITH THIS?
Aged One:
If this file was created by a Cobol program, there should not be spaces after the numbers, the numbers should have leading zeros as you expect. In this case the program which created the file is at fault.
If you are trying to convert a field with trailing spaces you might have to pass the field to a subroutine which redefines the field as a table of 1 character fields,
then works from right to left until it hits a non-space character, then starts transferring the rest of the characters to another zero filled table starting from right to left.
If the original field has a literal decimal point, then you will need logic to set the character index position of the receiving field as soon as you detect the decimal character.
*****************************************************************
***
*** REMOVE SPACES AND JUSTIFICATION AS REQUESTED
***
*** INPUT :
***
*** UP043-IN ALPHANUMERIC FIELD
*** UP043-LENGTH LENGTH OF FIELD
*** UP043-JUST JUSTIFICATION OF RETURNING FIELD
*** L : LEFT (DEFAULT)
*** R : RIGHT (UP043-LENGTH !!!!!)
*** C : CENTERED (UP043-LENGTH !!!!!)
***
*** RETURNING :
***
*** UP043-OUT WITHOUT SPACE AND JUSTIFIED AS REQUESTED
*** UP043-CHARS (NO. OF CHARACTERS)
***
*** USAGE IN PROGRAM :
*** ==================
*** MOVE WS-FIELD TO UP043-IN.
*** MOVE 'R' TO UP043-JUST.
*** COMPUTE UP043-LENGTH = FUNCTION LENGTH (WS-FIELD).
*** PERFORM UP043.
*** MOVE UP043-OUT TO WS-FIELD.
***
*****************************************************************
UP043 SECTION.
UP043-00.
MOVE ZERO TO UP043-CHARS.
MOVE SPACE TO UP043-OUT.
IF UP043-LENGTH = ZERO
OR UP043-LENGTH > UP043-MAX-I
MOVE UP043-MAX-I TO UP043-LENGTH.
IF UP043-JUST NOT = 'C'
AND NOT = 'R'
MOVE 'L' TO UP043-JUST.
IF UP043-IN = SPACE
GO TO UP043-EX.
IF UP043-JUST = 'R'
GO TO UP043-30.
UP043-10.
*** JUSTIFICATION LEFT
SET UP043-IN-I UP043-OUT-I TO 1.
PERFORM UNTIL UP043-IN-I > UP043-MAX-I
OR UP043-OUT-I > UP043-LENGTH
IF UP043-IN-X (UP043-IN-I) NOT = SPACE
MOVE UP043-IN-X (UP043-IN-I)
TO UP043-OUT-X (UP043-OUT-I)
ADD 1 TO UP043-CHARS
SET UP043-OUT-I UP BY 1
END-IF
SET UP043-IN-I UP BY 1
END-PERFORM.
IF UP043-JUST = 'L'
GO TO UP043-EX.
*** JUSTIFICATION CENTERED
IF UP043-CHARS = UP043-LENGTH
GO TO UP043-EX.
DIVIDE UP043-CALC-1 BY 2 GIVING
UP043-CALC-1 REMAINDER UP043-CALC-2.
ADD 1 TO UP043-CALC-1.
MOVE SPACE TO UP043-WS.
MOVE UP043-OUT TO UP043-WS (UP043-CALC-1 : ).
MOVE UP043-WS TO UP043-OUT.
GO TO UP043-EX.
UP043-30.
*** JUSTIFICATION RIGHT
SET UP043-IN-I TO UP043-MAX-I.
SET UP043-OUT-I TO UP043-LENGTH.
PERFORM UNTIL UP043-IN-I < 1
OR UP043-OUT-I < 1
IF UP043-IN-X (UP043-IN-I) NOT = SPACE
MOVE UP043-IN-X (UP043-IN-I)
TO UP043-OUT-X (UP043-OUT-I)
ADD 1 TO UP043-CHARS
SET UP043-OUT-I DOWN BY 1
END-IF
SET UP043-IN-I DOWN BY 1
END-PERFORM.
move zero to alfanumeric-thing.
perform varying sub-token from 1 by 1 until sub-token >
length of stuf-to-inspect
if stuf-to-inspect (sub-token:1) numeric
move alfanumeric-minus-one to alfanumeric-thing
move stuf-to-inspect (sub-token:1) to
alfanumeric-last-byte
end-if
end-perform.
**** with this (not tested coding, just giving you the idea) you can put the numeric parts of the field you are inspecting into the string which can be used as a numeric field by its redefined name numeric-field.
The compiler can give a warning about overlapping storage but it works on mainframe and pc platforms.
UNSTRING INPUT-STRING DELIMITED BY ALL SPACES
INTO R1 R2 R3
END-UNSTRING
This should end up with RESULT-FIELDS looking like:
"0002340009764200505"
The only part of the solution I'm not 100% on is the leading zeros in the result field (I don't have access to a mainframe at the moment), but I,m pretty sure that the compiler "unstrings" the input field according to "move"
rules (i.e. numeric alignment).
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.