MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Right-Justifying text in COBOL
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Tue Jun 22, 2004 3:13 pm    Post subject: Right-Justifying text in COBOL Reply with quote

Vini,

I tried you program, and it did appear to work. But when I modified it thus:
Code:
       WORKING-STORAGE SECTION.                                             
       01  RIGHT1     PIC X(40)  JUSTIFIED RIGHT.                           
       01  TEST1      PIC X(30)  VALUE 'A string of ... 30 characters.'.   

       PROCEDURE DIVISION.             
           MOVE TEST1  TO RIGHT1       
           DISPLAY 'TEST1...: ' TEST1   
           DISPLAY 'RIGHT1..: ' RIGHT1 
                                       
           MOVE '7 chars' TO TEST1     
           MOVE TEST1  TO RIGHT1       
           DISPLAY 'TEST1...: ' TEST1   
           DISPLAY 'RIGHT1..: ' RIGHT1 

I got this result:
Code:
TEST1...: A string of ... 30 characters.         
RIGHT1..:           A string of ... 30 characters.
TEST1...: 7 chars                                 
RIGHT1..:           7 chars                       

Very confusing! I can see how in the first part, the 30-byte string got right-justified into the 40-byte field, because there are 10 leading spaces.

But in the second part, I move a much shorter text constant to TEST1, so I would expect more spaces at the beginning of RIGHT1. But the DISPLAY shows the same 10 leading spaces.

What gives? I fear I have no idea how the RIGHT JUSTIFIED clause is supposed to work.

Any help from anyone would be most appreciated!

--Greg
Back to top
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Wed Jun 23, 2004 9:16 am    Post subject: Reply with quote

Greg , Interesting ! Try Initialising TEST1 & RIGHT1 before the 2nd series of Moves.
Back to top
View user's profile Send private message
coolman
Intermediate


Joined: 03 Jan 2003
Posts: 283
Topics: 27
Location: US

PostPosted: Wed Jun 23, 2004 10:37 am    Post subject: Reply with quote

Greg,

When you move a string data item which is smaller in length(7 chars) than the target, what apparently happens is that the default value(spaces) gets padded up to the right until the length of the datatypes match. So, per your example, 7 chars gets padded up with spaces until the length of 30 and hence when you display, you get the display at the same position.

To test this define another variable say x(10) and move '7 chars' to this variable and you would see what you expected.

Hope this helps...

Cheers,
Coolman
________
weed news


Last edited by coolman on Sat Feb 05, 2011 1:37 am; edited 1 time in total
Back to top
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Wed Jun 23, 2004 10:56 am    Post subject: Reply with quote

Greg, On second thots ... am sure the Intialise would not have helped ya.

Whats happening here is that as you are moving '7 chars' to a PIC X(30) which has a default Justification of LEFT. Essentially after the "MOVE '7 chars' TO TEST1 ", what TEST1 has again is nothing but a Left justified String of length 30 with trailing spaces.
Back to top
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Wed Jun 23, 2004 11:03 am    Post subject: Reply with quote

contd ...
Move the '7 Chars' constant Directly to RIGHT1 or Define an Item TEST2 of PIC X(7) and move '7 Chars' to this before moving it to RIGHT1.
Back to top
View user's profile Send private message
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Wed Jun 23, 2004 3:00 pm    Post subject: Reply with quote

OK, no I get it.... If I do this:
Code:
WORKING-STORAGE SECTION.                                         
01  RIGHT1     PIC X(40)  JUSTIFIED RIGHT.                       
01  TEST1      PIC X(30)  VALUE 'A string of ... 30 characters.'.
PROCEDURE DIVISION.             
    MOVE TEST1  TO RIGHT1       
    DISPLAY 'TEST1...: ' TEST1 
    DISPLAY 'RIGHT1..: ' RIGHT1
                               
    MOVE '7 chars' TO RIGHT1   
    DISPLAY 'TEST1...: ' TEST1 
    DISPLAY 'RIGHT1..: ' RIGHT1
Then I get this:
Code:
TEST1...: A string of ... 30 characters.         
RIGHT1..:           A string of ... 30 characters.
TEST1...: A string of ... 30 characters.         
RIGHT1..:                                  7 chars
This makes sense, but it doesn't help me do what I wanted, namely, to be able to right-justify ANY text string. It appears I have to use INSPECT and INSTECT FUNCTION REVERSE to do that...

Thanks, All!

--Greg
Back to top
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Wed Jun 23, 2004 3:34 pm    Post subject: Reply with quote

Greg , I guess your approach would depend on the Source of your ANY string .
If it originates from a Db Table you can still manage by trimming the Trailing spaces by using a scalar Function before moving to the Justified Right Item and Displaying it.
Curious , why you would always want to move the String constant (if this is the source in your case) to an Item of "fixed" length first , instead of the constant directly to the Justified Right Item. Only thing I see is you may want to avoid hard-coding and hence define it in working-storage instead , which is a good practice indeed but you can still keep the length of the Item to its "actual" length in which case.
You may be thinking in terms of having a Sub-program and hence its linkage parms then it makes more sense , yeah.
Back to top
View user's profile Send private message
slade
Intermediate


Joined: 07 Feb 2003
Posts: 266
Topics: 1
Location: Edison, NJ USA

PostPosted: Thu Jun 24, 2004 3:10 pm    Post subject: Reply with quote

Hi Greg,

Justified (right or left) only works when using fields of differing DEFINED lengths, e.g.:
Code:

a   pic x(04) value '0006'.
b   pic x(10) just right. 

move a to b

will yield 'bbbbbb0006' in field b (bb...bb are spaces).

On the other hand, it will yield:
Code:

'bbbbbb6bbb' if a is  pic x(04) value '6'.

Regards, Jack.
Back to top
View user's profile Send private message
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Fri Jun 25, 2004 2:48 pm    Post subject: Reply with quote

Vini said:
Quote:
You may be thinking in terms of having a Sub-program and hence its linkage parms then it makes more sense , yeah.
Vini, you hit the proverbial nail on the head. I was actually writing a subpgm to center any text string on any passed string length. The linkage parms would have a large in/out text string, so I needed to compute both leading and trailing spaces to get the correct non-null string length.

I originally used INSPECT parm-string FOR LEADING SPACE and INSPECT FUNCTION REVERSE (parm string) FOR LEADING SPACE, etc. The I got to wondering if it would be better (i.e., easier, more efficient, more elegant, etc.) to first right-justify the string and srop the second INSPECT.

Well, I ran into problems getting the string to right-justify!

Anyway, I got sidetracked and haven't been back to my centering issue/project/problem (it wasn't strickly work-related, more just trying to figure out the best way to do it if and when it came up....) I'm now leaning away from the supgm approach, and thinking it may be better to use copybook code, mainly so I don't have to have a huge string parm and a "how much of the string to use" parm. Next week I may post a separate message about this to solicit some input.

Thanks!

--Greg
Back to top
View user's profile Send private message
ranga_subham
Intermediate


Joined: 31 Jan 2006
Posts: 255
Topics: 72

PostPosted: Fri Mar 21, 2008 6:16 am    Post subject: what is wrong in this? Reply with quote

Hi,

I wrote a small program to know how "JUST RIGHT" clause works. Confused Shocked

Code:

IDENTIFICATION DIVISION.                 
PROGRAM-ID. INSPECT1.                   
ENVIRONMENT DIVISION.                   
DATA DIVISION.                           
WORKING-STORAGE SECTION.                 
01 WS-VAR  PIC X(20) VALUE 'NEW YORK NY'.
01 WS-VA1  PIC X(20) JUST RIGHT.         
PROCEDURE DIVISION.                     
MAIN-PARA.                               
    DISPLAY '*** PROGRAM STARTS ***'.   
    DISPLAY WS-VAR.                     
    MOVE SPACES TO WS-VA1.               
    MOVE WS-VAR TO WS-VA1.               
    DISPLAY WS-VA1.                     
    DISPLAY WS-VA1(18:2).               
    DISPLAY '*** PROGRAM ENDS ***'.
    STOP RUN.                       


I expected the following display in the SYSOUT:

Code:

*** PROGRAM STARTS ***
NEW YORK NY
           NEW YORK NY
NY
*** PROGRAM ENDS ***


But it is showing the following display in SYSOUT Exclamation Exclamation Exclamation

Code:

*** PROGRAM STARTS ***
NEW YORK NY           
NEW YORK NY           
                     
*** PROGRAM ENDS ***


Can anybody please explain me what is wrong here Question Question Question

bonk stars curse grrr

Thanks.
_________________
Ranga
*****
None of us is as smart as all of us - Ken Blanchard
Back to top
View user's profile Send private message
ranga_subham
Intermediate


Joined: 31 Jan 2006
Posts: 255
Topics: 72

PostPosted: Fri Mar 21, 2008 6:17 am    Post subject: Reply with quote

We are using "IBM Enterprise COBOL for z/OS 3.4.1".....

Thx.
_________________
Ranga
*****
None of us is as smart as all of us - Ken Blanchard
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Mar 21, 2008 10:31 am    Post subject: Reply with quote

ranga_subham,

pay attention to what SLADE said in here

http://www.mvsforums.com/helpboards/viewtopic.php?p=11719#11719


Try changing the lengths as follows and re-run your program.

Quote:

01 WS-VAR PIC X(20) VALUE 'NEW YORK NY'.
01 WS-VA1 PIC X(30) JUST RIGHT.

_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Nithisha
Beginner


Joined: 27 May 2006
Posts: 23
Topics: 13
Location: Pune

PostPosted: Mon Mar 24, 2008 6:26 am    Post subject: Reply with quote

Hi Friend

I have test and found that the justified field (recieving field)shoule be greater than the sending field.Then right justify is working.
Code:

       IDENTIFICATION DIVISION.                                                 
       program-id.  sample1.                                                   
       ENVIRONMENT DIVISION.                                                   
       DATA DIVISION.                                                           
       WORKING-STORAGE SECTION.                                                 
       01  WS-DATA   PIC  X(40) JUSTIFIED RIGHT.                               
       01  WS-DATA1  PIC  X(30) VALUE 'SATHISH'.                               
       PROCEDURE DIVISION.                                                     
       MAIN-PARA.                                                               
           DISPLAY '"THIS IS NEW PROGRAM"'                                     
           DISPLAY WS-DATA1 .                                                   
           MOVE SPACE TO WS-DATA.                                               
           DISPLAY WS-DATA.                                                     
           MOVE WS-DATA1 TO WS-DATA .                                           
           DISPLAY WS-DATA.                                                     
           STOP RUN.
Back to top
View user's profile Send private message AIM Address
ranga_subham
Intermediate


Joined: 31 Jan 2006
Posts: 255
Topics: 72

PostPosted: Mon Mar 24, 2008 7:02 am    Post subject: Reply with quote

Thank you people......I got my basics right..... Embarassed
_________________
Ranga
*****
None of us is as smart as all of us - Ken Blanchard
Back to top
View user's profile Send private message
slade
Intermediate


Joined: 07 Feb 2003
Posts: 266
Topics: 1
Location: Edison, NJ USA

PostPosted: Thu Apr 03, 2008 2:15 pm    Post subject: Reply with quote

Hi Gang,

This might work. Haven't tested, but the concept looks OK.

Also, didn't test for zero length and other things around the edges.
Code:
 WORKING-STORAGE SECTION.                                             
01  RIGHT1     PIC X(40) JUST  RIGHT.                                 
01  TEST1      PIC X(40) VALUE SPACES.                               
                                                                     
01  ADDTL-FLDS.                                                       
    05  WS-NBR-OF-SPACES  PIC S9(004).                               
    05  WS-STRING-LEN     PIC S9(004).                               
                                                                     
PROCEDURE DIVISION.                                                   
    MOVE '1234'            TO TEST1                                   
    PERFORM CALC-MOVE-AND-DISP-STRING                                 
    MOVE '1234567890ABCDE' TO TEST1                                   
    PERFORM CALC-MOVE-AND-DISP-STRING
    GOBACK
     .                                                                 
CALC-MOVE-AND-DISP-STRING.                                           
    MOVE ZERO TO WS-NBR-OF-SPACES                                     
                 WS-STRING-LEN                                       
                                                                     
    INSPECT FUNCTION REVERSE(TEST1)                                   
            TALLYING WS-NBR-OF-SPACES                                 
            FOR      LEADING SPACES                                   
                                                                     
    COMPUTE WS-STRING-LEN = LENGTH OF TEST1 - WS-NBR-OF-SPACES       
                                                                     
    MOVE TEST1(1:WS-STRING-LEN) TO  RIGHT1                           
    DISPLAY 'TEST1...: ' TEST1                                       
    DISPLAY 'RIGHT1..: ' RIGHT1                                       
    .

_________________
Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group