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!

remove trailing spaces in address fields

Status
Not open for further replies.

hugheskbh

Programmer
Dec 18, 2002
37
US
I need to remove trailing spaces in address fields. Please tell me how to accomplish this in cobol. This is very urgent.

Thanks

Ken Hughes
 
A tip: Usually using the word "urgent" in your posts will make people want to answer your post twice as slow.

In short, scan for the first non-space character from the end of the X field and then positional move that section MOVE FIELD-1 (1:COUNTER) TO Whatever.
 
You may also play with the UNSTRING statement.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ken,

You'll find that thread209-858815 covers this in detail, giving various solutions.

Marc
 
Hi,

I'm very sorry for my lack of knowledge on this site, but I'm still having problems. All I want to do is to scan a field and remove the trailing spaces. Please give me a quick example of how to do this. For example, an address field where the field length is 40 characters, but the field does not always use 40 characters, how can I remove the trailing spaces. Please give me an example.
 
have a look at the thread mentioned. It has all you need top know for the problem you raised.

Try the code supplied, and if you still have problems then post back with your code, your sample data, and whatever problems you are having.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
I remember one solution long ago I had put. Dont have COBOL to test it now.

Define two variables. one is exactly of double the length of the size of the receiving variable (in your case receiving field is PIC X(40). so this variable will be of PIC X(80).)

Define a variable of equal length of your receiving variable and initialise it to spaces. So here it goes.

01 ADDRESS PIC X(40) (This will hold address value
without spaces)
01 TEMP-ADDRESS PIC X(80) value spaces.
01 TEMP-DELIMETER Pic x940) values spaces.
01 CURRENT-ADDRESS PIC x(40). (This has the address values with spaces.)

MOVE CURRENT-ADDRESS TO TEMP-ADDRESS.
UNSTRING TEMP-ADDRESS DELIMITED BY TEMP-DELIMTER INTO ADDRESS.

This unstring statement should able to pick up the text part of the address field by removing the trailer spaces.

Let me know if it works.

Devesh
 
Ken,
The easiest way to do this is to flip the field around and then count the leading spaces (which are of course the trailing spaces as you've flipped the field). You then know the number of spaces at the end of the field and can substract that figure from the length of the field in order to give you the exact length of the data. Using reference modification, you can extract the data you are after.

Code:
INSPECT REVERSE(WS-ADDR-1) TALLYING WS-TALLY FOR LEADING SPACES

COMPUTE WS-DATA-LENGTH = LENGTH OF WS-ADDR-1 - WS-TALLY

MOVE WS-ADDR-1(1:WS-DATA-LENGTH) TO ...........

If you are attempting to concatenate more than one address line, then the STRING statement might be of more use.

Marc
 
Is it not true, that if the receiveing field is bigger then what is being moved into it then it will be filled with spaces.

So even if he takes his 40 byte addesss and finds the last non-space character and moves it to another 40 byte field that spaces will return?

So nothing would be gained........

What is the reason that the trailing spaces need to be removed?
 
Ken,

Davesh is almost there.
Code:
01 SCAN-ADDRESS VALUE SPACES.
   02 TEMP-ADDRESS     PIC X(40).
   02 TEMP-DELIMITER   Pic X(40).

01 I    pic 99.

01 CURRENT-ADDRESS  PIC x(40). (This has the address values with spaces.)
...
MOVE CURRENT-ADDRESS TO TEMP-ADDRESS.
MOVE 0 TO I

INSPECT SCAN-ADDRESS 
    TALLYING I FOR CHARACTERS
    BEFORE INITIAL TEMP-DELIMITER.

IF I = 0
    [i]address is spaces[/i]
ELSE
    MOVE CURRENT-ADDRESS (1:I) TO [i]whatever[/i]
END-IF

So, Ken, what will you be doing with the result of this computation?

Tom Morrison
 
I'm guessing that Ken is attempting to concatenate various lines of an address into one field - hence my earlier comment about STRING.

Ken?

Marc
 
inspect function reverse(variable). . .

The idea is to count the varable backwards until you reach the first non trailing space and figure it out from there.

Example:

ADDRESS-EDIT.
MOVE ZERO TO BLK-CTR, TEMP-CTR.
INSPECT FUNCTION REVERSE(WS-ADDRESS-1)
TALLYING BLK-CTR FOR LEADING SPACES.
MOVE WS-ADDRESS-1 TO WS-ADDRESS.
COMPUTE TEMP-CTR = (30 - BLK-CTR).
ADD 1 TO TEMP-CTR.
MOVE ', ' TO WS-ADDRESS (TEMP-CTR:2).
ADD 3 TO TEMP-CTR.
MOVE ZERO TO BLK-CTR.
INSPECT FUNCTION REVERSE(WS-CITY)
TALLYING BLK-CTR FOR LEADING SPACES.
MOVE WS-CITY (1:(19 - BLK-CTR)) TO
WS-ADDRESS (TEMP-CTR:(19 - BLK-CTR)).
COMPUTE TEMP-CTR = (TEMP-CTR + (19 - BLK-CTR)).
MOVE ZERO TO BLK-CTR.
MOVE ', ' TO WS-ADDRESS (TEMP-CTR:2).
ADD 2 TO TEMP-CTR.
MOVE WS-STATE TO WS-ADDRESS (TEMP-CTR:2).
ADD 3 TO TEMP-CTR.
IF WS-ZIP-2 = SPACES MOVE WS-ZIP-1 TO
WS-ADDRESS (TEMP-CTR:5)
ELSE
MOVE WS-ZIP-1 TO WS-ADDRESS (TEMP-CTR:5)
MOVE '-' TO WS-ADDRESS ((TEMP-CTR + 5):1)
MOVE WS-ZIP-2 TO WS-ADDRESS ((TEMP-CTR + 6):4).


If you do not like my post feel free to point out your opinion or my errors.
 
What I was trying to do in the example is remove the trailing spaces and then concatanate the fields. In the example My address field had a 30 character field with the Street Address, a city field, a state (2 char abbreviation) field and a Zip field with the 9 digit zip code. So I used address modification to move the part of the field that I wanted to move.

When I say move field-1 (1:12) to field-2 (10:12) it allows you to move a portion of one field into a portion of a second field without replacing the remaing or unused part of the second field.

field-1 (1:12) is field one first position with 12 characters.

field-2 (10:12) is the resulting field starting at the 10th position for 12 characters. No other part of field-2 is disturbed or replaced.

If you do not like my post feel free to point out your opinion or my errors.
 
here's an example on what I did. Since our toolset does not
allow us to enter some functions like STRING and SCAN, here is a long version of how to do it. This is to concatenate Last-Name, First-Name and Middle-Init fields. The output is
XXX-STRING-OUT which is 100 char long and you can always cut the size the way you want it.

01 XXX-CONCAT-ROUTINE.
02 XXX-INPUT-DATA PIC X(100) VALUE SPACES.
02 XXX-INPUTS REDEFINES XXX-INPUT-DATA.
03 XXX-INPUTX OCCURS 100 TIMES.
04 XXX-INPUT PIC X(1).
02 XXX-STRING-OUT PIC X(100) VALUE SPACES.
02 I1 PIC 9(03) VALUE ZEROES.
02 I2 PIC 9(03) VALUE ZEROES.


sOMEWHERE IN MY PROCEDURE:

PERFORM 6000-CONCAT-ROUTINE.

6000-CONCAT-ROUTINE SECTION 55.
*****************************************************************
6000-START.

* Count Characters in fields.

INITIALIZE XXX-CONCAT-ROUTINE.

MOVE XXX-LAST-NAME TO XXX-INPUT-DATA.
MOVE ZEROES TO I1.
MOVE ZEROES TO I2.

PERFORM
VARYING I1 FROM 100 BY -1
UNTIL (I1 = 0)
OR (I1 < I2)
IF (XXX-INPUT (I1) NOT = SPACES)
MOVE I1 TO I2
END-IF
END-PERFORM.

ADD 1 TO I2.
MOVE "," TO XXX-INPUT-DATA(I2:1).
ADD 2 TO I2.
MOVE XXX-INPUT-DATA TO XXX-STRING-OUT.
MOVE XXX-FIRST-NAME(1:15) TO XXX-STRING-OUT(I2:15).

MOVE SPACES TO XXX-INPUT-DATA.
MOVE XXX-STRING-OUT TO XXX-INPUT-DATA.
MOVE SPACES TO XXX-STRING-OUT.
MOVE ZEROES TO I1.
MOVE ZEROES TO I2.

PERFORM
VARYING I1 FROM 100 BY -1
UNTIL (I1 = 0)
OR (I1 < I2)
IF (XXX-INPUT (I1) NOT = SPACES)
MOVE I1 TO I2
END-IF
END-PERFORM.

ADD 2 TO I2.
MOVE XXX-INPUT-DATA TO XXX-STRING-OUT.
MOVE XXX-MIDDLE-INIT(1:1) TO XXX-STRING-OUT(I2:1).

DISPLAY XXX-STRING-OUT.

6000-END.


 
If all he is wanting to do is concat some field then "STRING" is the way to go.

Code:
STRING ADDRESS1    delimited by spaces
       ', '        delimited by size
       address2    delimited by spaces
       space       delimited by size
       City        delimited by spaces
       ', '        delimited by size
       State       delimited by spaces
       space       delimited by size
       ZIP-Code    delimited by spaces
  into ONE-BIG-ADDRESS
END-STRING.

This will trim the extra spaces and concat them together.
 
kkitt -

Your approach fails to recognize embedded spaces that are present in most addrresses (e.g. 1 Microsoft Way, Redmond, WA).

Glenn
 
That's why I use
Code:
delimited by "   "
that's 3 spaces, and highly unlikely to be used in real addresses ;-)

HTH
 
If the requirements are to remove ANY extra spaces, e.g. convert every occurrence of 2 or more spaces to just ONE space then one solution will be



Code:
01 v1.
   05 v2 pic x(200) occurs 20.
01 v-orig pic x(200)
01 v-dest pic x(200).
01 v-pointer pic 9(4).
01 v-x pic 9(4).

move 1 to v-pointer
move 1 to w-x.
if v-orig not = spaces
   perform until v-pointer > 200
      unstring v-orig delimited by all spaces
         into v2(w-x)
         pointer v-pointer
      add 1 to v-x
   end-perform
end-if.

move 1 to v-pointer
if v-orig not = spaces
   perform varying w-x from 1 by 1
     until v2(w-x) not = spaces
        string v2(w-x) delimited by spaces.
               " " delimited by size
          into v-dest
         pointer v-pointer
   end-perform
end-if.

or

Code:
01 VOUT          PIC X(200).
01 VWORK         PIC X(200).
01 VTEMP         PIC X(200).
01 p1 pic 9999.
01 p2 pic 9999.
move 1 to p1 p2.
move spaces to vout vtemp.
perform until p1 > 200
   unstring vwork delimited by all spaces
       into vtemp
    pointer p1
   if vtemp not = spaces
      string vtemp delimited by spaces
             " " delimited by size
        into vout
      pointer p2
   end-if
end-perform.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top