Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW DO I HANDLE NUMERIC PC FIELDS

Status
Not open for further replies.

aged1

Instructor
Jan 11, 2003
2
AU
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.

123456ssss
||||||___
|||||___ |
||||___ ||
|||___ |||
||___ ||||
|___ |||||
||||||
0000123456

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.


 
Hi Aged1,

probably the following subroutine can help you.

*****************************************************************
***
*** 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.

COMPUTE UP043-CALC-1 = UP043-LENGTH - UP043-CHARS.

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.

GO TO UP043-EX.


UP043-EX.
EXIT.



 
Oops, I forgot the field declarations in the working storage.

Here they are : *****************************************************************
***
*** FIELD DEFINITIONS FOR SUB-ROUTINE UP043
*** ----------------------------------------
***
*** INPUT : UP043-IN
*** UP043-LENGTH LENGTH OF INPUT FIELD
*** UP043-JUST L : JUSTIFICATION LEFT
*** R : JUSTIFICATION RIGHT
*** C : JUSTIFICATION CENTERED
***
***
*** RETURNS : UP043-OUT
*** UP043-CHARS NO. OF CHARACTERS
***
*****************************************************************
01 UP043-DEFINITIONS.
05 UP043-IN.
10 FILLER OCCURS 80 INDEXED BY UP043-IN-I.
15 UP043-IN-X PIC X.
05 UP043-OUT.
10 FILLER OCCURS 80 INDEXED BY UP043-OUT-I.
15 UP043-OUT-X PIC X.
05 UP043-WS.
10 FILLER OCCURS 80 INDEXED BY UP043-WS-I.
15 UP043-WS-X PIC X.
05 UP043-JUST PIC X VALUE SPACE.
05 UP043-MAX-I PIC S9(3) COMP-3 VALUE 80.
05 UP043-LENGTH PIC S9(3) COMP-3 VALUE ZERO.
05 UP043-CHARS PIC S9(3) COMP-3 VALUE ZERO.
05 UP043-CALC-1 PIC S9(3) COMP-3 VALUE ZERO.
05 UP043-CALC-2 PIC S9(3) COMP-3 VALUE ZERO.




 
Hi,

to do it the easy way, you define something like this:

01 alfanumeric-thing.
03 filler pic x.
03 alfanumeric-minus-one.
05 filler pic x(17).
05 alfanumeric-last-byte pic x.
01 numeric-field redefines alfanumeric-thing pic 9(18).

01 sub-token pic s9(4) comp sync.

...

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.

Enjoy!


 
EACH OF YOU HAVE BEEN MOST HELPFUL, THANKS
 
Hi Aged1,

I know this reply is late, but for posterity here goes...

If your COBOL compiler has an UNSTRING statement, this may be an easy solution:

If the string in question looks something like this:
&quot;234 97642 505 &quot;

It can be defined as:
01 INPUT-STRING PIC X(019).

The result field as:

01 RESULT-FIELDS.
05 R1 PIC 9(006).
05 R2 PIC 9(008).
05 R3 PIC 9(005).

Proc Div code:

UNSTRING INPUT-STRING DELIMITED BY ALL SPACES
INTO R1 R2 R3
END-UNSTRING

This should end up with RESULT-FIELDS looking like:

&quot;0002340009764200505&quot;

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 &quot;unstrings&quot; the input field according to &quot;move&quot;
rules (i.e. numeric alignment).

Jack

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top