Page 1 of 1

get off spaces to the last of a string

PostPosted: Mon Jul 26, 2010 11:08 pm
by vegafacundodaniel
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

Re: get off spaces to the last of a string

PostPosted: Mon Jul 26, 2010 11:23 pm
by Robert Sample
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.

Re: get off spaces to the last of a string

PostPosted: Mon Jul 26, 2010 11:43 pm
by NicC
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.

Re: get off spaces to the last of a string

PostPosted: Tue Jul 27, 2010 1:00 am
by dick scherrer
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.

Re: get off spaces to the last of a string

PostPosted: Tue Jul 27, 2010 4:49 pm
by vegafacundodaniel
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

Re: get off spaces to the last of a string

PostPosted: Tue Jul 27, 2010 5:23 pm
by Robert Sample
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.

Re: get off spaces to the last of a string

PostPosted: Tue Jul 27, 2010 6:37 pm
by vegafacundodaniel
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 !