get off spaces to the last of a string



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

get off spaces to the last of a string

Postby vegafacundodaniel » Mon Jul 26, 2010 11:08 pm

Hello,

A question please. I have a field x(10). Inside I can have spaces at the last and I don't want them.

ex:
ABCDEFGHIJ -----------> ABCDEFGHIJ ---- OK

But:

ABCDEF + space + space + space + space --------------> I only want to obtain the string ABCDEF
I must read the field from the last character to the first one, until I find a not space

Thanks
vegafacundodaniel
 
Posts: 61
Joined: Tue Jul 20, 2010 4:27 pm
Has thanked: 1 time
Been thanked: 0 time

Re: get off spaces to the last of a string

Postby Robert Sample » Mon Jul 26, 2010 11:23 pm

COBOL, unlike C or similar languages, requires variables to be fixed in length (except for OCCURS DEPENDING ON in LINKAGE SECTION or in an 01 under an FD). If you define a variable as PIC X(10) and it is in WORKING-STORAGE, it will contain 10 characters -- always. They may be spaces, or LOW-VALUES, or HIGH-VALUES, or printing characters, or non-printing characters -- but there will always be 10 characters in the variable. Accordingly, when you say
I can have spaces at the last and I don't want them.
what do you want them to be if not spaces? They will exist, they will be characters, and they cannot be removed -- period. This is the way COBOL is.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: get off spaces to the last of a string

Postby NicC » Mon Jul 26, 2010 11:43 pm

If you need to extract only the data and leave trailing spaces behind then, yes, INSPECT TALLYING the reversed string to find the last non-blank character and then use refernce modification to extract only the bytes up to the last data byte.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: get off spaces to the last of a string

Postby dick scherrer » Tue Jul 27, 2010 1:00 am

Hello,

How do you intend to use this "new" value (without the spaces)?

If you post what you are trying to accomplish (rather than how you want the code to work), someone may have a suggestion.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: get off spaces to the last of a string

Postby vegafacundodaniel » Tue Jul 27, 2010 4:49 pm

Hello,

Example :

Field : STREET X(10).
Value : STREET = 'XXXX '

I am using:
STRING STREET DELIMITED BY ' '
' ' DELIMITED BY SIZE
COUNTRY DELIMITED BY ' '
INTO ADDRESS

But 2 spaces (' ') for delimite the street may be wrong. I need to read from the last to the begining until I find the first character not space.

Thanks in advance
vegafacundodaniel
 
Posts: 61
Joined: Tue Jul 20, 2010 4:27 pm
Has thanked: 1 time
Been thanked: 0 time

Re: get off spaces to the last of a string

Postby Robert Sample » Tue Jul 27, 2010 5:23 pm

You can either do a PERFORM from 10 to 1 looking for the first non-space character via reference modification, or you can use the REVERSE function and have your PERFORM go from 1 to 10. You need an index variable but the code is not tricky to write.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: get off spaces to the last of a string

Postby vegafacundodaniel » Tue Jul 27, 2010 6:37 pm

Thanks to all !!!

I found myself that I wanted with the help NIcC's:

INSPECT FIELD
TALLYING AMOUNT-CHAR
FOR CHARACTERS BEFORE INITIAL SPACE.

STRING FIELD(1:AMOUNT-CHAR) DELIMITED BY SIZE
....
....

Regards !
vegafacundodaniel
 
Posts: 61
Joined: Tue Jul 20, 2010 4:27 pm
Has thanked: 1 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post