Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

remove trailing spaces in address fields

hugheskbh (Programmer)
17 Mar 06 18:48
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
Glenn9999 (Programmer)
18 Mar 06 4:55
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.
PHV (MIS)
18 Mar 06 5:04
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

MarcLodge (Programmer)
18 Mar 06 6:23
Ken,

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

Marc
hugheskbh (Programmer)
18 Mar 06 19:51
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.  
fredericofonseca (IS/IT--Management)
18 Mar 06 20:19
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
www.syssoft-int.com

deveshjogal (Programmer)
19 Mar 06 23:17
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
MarcLodge (Programmer)
20 Mar 06 4:38
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
kkitt (Programmer)
20 Mar 06 11:08
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?
k5tm (Programmer)
20 Mar 06 11:10
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
    address is spaces
ELSE
    MOVE CURRENT-ADDRESS (1:I) TO whatever
END-IF

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

Tom Morrison
www.liant.com

MarcLodge (Programmer)
20 Mar 06 11:17
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
ceh4702 (Programmer)
20 Mar 06 11:47
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.

ceh4702 (Programmer)
20 Mar 06 12:04
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.

jmanj (Programmer)
20 Mar 06 12:38
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.


kkitt (Programmer)
20 Mar 06 14:28
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.
3gm (Programmer)
20 Mar 06 14:42
kkitt -

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

Glenn
TonHu (Programmer)
20 Mar 06 18:31
That's why I use

CODE

delimited by "   "
that's 3 spaces, and highly unlikely to be used in real addresses winky smile

HTH
fredericofonseca (IS/IT--Management)
20 Mar 06 20:01
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
www.syssoft-int.com

k5tm (Programmer)
21 Mar 06 9:30
So, Ken, can you return to us?  It would be good to know whether your question has been answered.  ponder

Tom Morrison
www.liant.com

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close