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 

COBOL: How to remove leading spaces from an alpha field.

 
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: Fri Jun 04, 2004 10:18 am    Post subject: COBOL: How to remove leading spaces from an alpha field. Reply with quote

To all,

I have been trying to find a simple, elegant way to remove the leading spaces from an alpha field, and I'm striking out big-time.

It appears I'm going to have to redefine the alpha field as an array of single characters (shades of C!!) and loop though the array, doing byte-by-byte comparisons and moving to another array.

This really seems quite clunky; surely there's a better way?

If not, I'll probably dump the code into a subprogram and just call it....

Any help would be greatly appreciated!

--Greg
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Fri Jun 04, 2004 11:01 am    Post subject: Reply with quote

You don't have redefine and check each and every byte to remove the leading spaces. You can simply use the Function UNSTRING and remove the spaces.

Code:

01 WS-STR                PIC X(20) VALUE '          BBXXXYYYY'. 
01 WS-SPACE               PIC X(20).                       
01 WS-FIELD               PIC X(20).                       

UNSTRING WS-STR DELIMITED BY ALL SPACES INTO   
         WS-SPACE,WS-FIELD 
                             
DISPLAY 'WS-SPACE:'  WS-SPACE                   
DISPLAY 'WS-FIELD:'  WS-FIELD


Upon execuetion of this you will have

Code:

WS-SPACE:
WS-FIELD:BBXXXYYYY


Hope this helps...

Cheers

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


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Fri Jun 04, 2004 1:14 pm    Post subject: Reply with quote

Kolusu,

Unfortunately, this does ot work if there are embedded spaces in the text string.

I did find a fairly useful alternative:
Code:
01 RIGHTY  PIC X(40) VALUE '    Text with leading spaces.'.
01 LEFTY   PIC X(40).
01 I       PIC 9(4) USAGE BINARY.

INSPECT RIGHTY TALLYING I FOR LEADING SPACE.           
MOVE RIGHTY ( I + 1 : LENGTH OF RIGHTY - I ) TO LEFTY
                             
DISPLAY 'RIGHTY:' RIGHTY       
DISPLAY 'LEFTY: ' LEFTY

It requires an additional tally field, but with only two statements, it's not too bad....

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


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

PostPosted: Fri Jun 04, 2004 1:14 pm    Post subject: Reply with quote

I guess Gregs situation is one where the number of Leading Blanks is a variable figure ...if not then the above solution is quite elegant.
Back to top
View user's profile Send private message
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Wed Jun 09, 2004 8:13 am    Post subject: Reply with quote

Vini is correct: The leading blanks can be variable. What I actually need to do is embed left-justified counters in text messages at the end of a job. For example:
Code:
NUMBER OF INPUT RECORDS READ: 7101     
NUMBER OF OUTPUT RECORDS WRITTEN: 7099
NUMBER OF FILES READ: 1               

The counters at the end of each line are numeric variables, but they must be left-justified in the text (this is a user requirement). If I use numeric masks with supression on the leading zeroes (i.e., PIC ZZZZ9), the text is still right-justified. In essense, I had to convert right-justified text to left-justified text.

Kolusu's UNSTRING suggestion would have worked just fine in this specific case, since the counters don't have embedded blanks. But I was trying to come up with a more-widely-applicable approach, one that could be used regardless of the text string's content or the number of leading blanks.

Thanks for the replies, everyone!

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


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

PostPosted: Mon Jun 14, 2004 4:28 pm    Post subject: Reply with quote

Gkreth,

Would JUST clause not help ?

Cheers,
Coolman.
________
Ford River Rouge Complex


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
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Tue Jun 15, 2004 3:54 pm    Post subject: Right-Justify text (was: How to remove leading spaces from a Reply with quote

Quote:
Gkreth,
Would JUST clause not help ?
Cheers,
Coolman.


Coolman,

I'm not sure how it would help in LEFT-justifying some text, but it does bring up an interesting question/issue: I cannto seem to get the JUSTIFIED RIGHT clause to work correctly. Consider the following:
Code:
WORKING-STORAGE SECTION.                             
01  ORIG                  PIC X(80)  VALUE SPACE.   
01  RIGHT1                PIC X(80)  JUSTIFIED RIGHT.
01  TEST1                 PIC X(80)  VALUE SPACE.   
                                                     
PROCEDURE DIVISION.                                 
    MOVE 'Some left-justified text' TO ORIG         
    MOVE ORIG   TO RIGHT1                           
    MOVE RIGHT1 TO TEST1                             
    DISPLAY 'ORIG....: ' ORIG                       
    DISPLAY 'RIGHT1..: ' RIGHT1                     
    DISPLAY 'TEST1...: ' TEST1                       

This give me the following output:
Code:
ORIG....: Some left-justified text 
RIGHT1..: Some left-justified text 
TEST1...: Some left-justified text 

Does anyone have any idea what I'm doing wrong?

Thanks,

--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 16, 2004 3:30 pm    Post subject: Reply with quote

gkreth ,
Code:
Try this
WORKING-STORAGE SECTION.                             
01  RIGHT1                PIC X(30)  JUSTIFIED RIGHT.
01  TEST1                 PIC X(20)  VALUE 'YOUR STRING OF 20 CHARS'.   
                                                     
PROCEDURE DIVISION.                                 
    MOVE TEST1  TO RIGHT1                           
               
    DISPLAY 'RIGHT1..: ' RIGHT1                     
                     


I havent tested , but it should give you following ..with 10 leading spaces with your text visibly right justified.

RIGHT1..:bbbbbbbbbbYOUR STRING OF 20 CHARS.

It seems the JUSTIFIED clause does not affect initial data settings (i.e., as specified by the VALUE clause). Its effective when you move an Item to a justified Item and when the Item lengths are different.
Back to top
View user's profile Send private message
gkreth
Beginner


Joined: 28 May 2004
Posts: 29
Topics: 10

PostPosted: Tue Jun 22, 2004 3:04 pm    Post subject: Reply with quote

Vini,

I started a new thread on this, because it's confusing the heck outta me..... See the next message --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 10:00 am    Post subject: Reply with quote

Greg, Good to hear from ya , was wondering what happened , just had no time to test any of this stuff .It did confuse the heck out of me too when I had wanted to use it first time last year sometime ,had given up on it then. Seeing your post , got keen again though.
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
Page 1 of 1

 
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