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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STRING removing leading spaces? 2

Status
Not open for further replies.

Nagrom

Programmer
Jan 28, 2002
234
0
0
US
Is there an easy way to concatenate two strings and ignore leading spaces in the concatenation? For example, how could you string "How" and " dy" to get "Howdy" as opposed to "How dy"?
 
Hi,

I haven't tried this and it's messy, but it sounds like it should work. Get your 2 fields contiguous as in ws-string-fld.
Code:
77 ws-len            pic s9(004) comp.
77 ws-ptr            pic s9(004) comp.

 
01 ws-string-fld.
   05  ws-how        pic  x(003).
   05  ws-dy         pic  x(004).

01 ws-howdy          pic  x(005).

    move +1  to ws-ptr ws-len
    unstring ws-string-fld 
        into ws-howdy delimited 
          by all ' ' 
       count ws-len pointer ws-ptr     
    end-unstring  

    unstring ws-string-fld 
        into ws-howdy(ws-len + 1:) delimited 
          by all ' ' pointer ws-ptr
    end-unstring
You can also do it with reference modification, something like this:
Code:
    move + 1 to in-ptr out-ptr 
    perform 
      until ws-string-fld(in-ptr:1)        = ' '
         or in-ptr > length of ws-string-fld 
            if ws-string-fld(in-ptr:1) not = ' '
               move ws-string-fld(in-ptr:1) 
                 to ws-howdy    (out-ptr:1)
               add  +1 to out-ptr
            end-if
            add  +1    to in-ptr
    end-perform
HTH, Jack.


 
Hi,

an easy construction to remove leading space is:

Code:
01  remove-space-field.
    03  rsf-leading-space       pic x.
    03  rsf-rest                pic x(100).

    move subject-of-interest to remove-space-field.
    if remove-space-field not = space
        perform until rsf-leadings-space not = space
            move rsf-rest to remove-space-field
        end-perform
    end-if.

Regards,

Crox
 
Another option, without the use of PERFORM:
Code:
01  IN-PTR    PIC 999.
01  OUT-PTR   PIC 999.
01  HOW       PIC X(10)  VALUE "    how  ".
01  DY        PIC X(10)  VALUE "    dy   ".
01  HOWDY     PIC X(20).
...
MOVE 1 to OUT-PTR.
MOVE SPACES TO HOWDY.
if HOW not equal SPACES
    MOVE 1 to IN-PTR
    INSPECT HOW TALLYING IN-PTR FOR LEADING SPACES
    STRING HOW (IN-PTR:) DELIMITED BY SPACE
        INTO HOWDY POINTER OUT-PTR
end-if.
if DY not equal SPACES
    MOVE 1 to IN-PTR
    INSPECT DY TALLYING IN-PTR FOR LEADING SPACES
    STRING DY (IN-PTR:) DELIMITED BY SPACE
        INTO HOWDY POINTER OUT-PTR
end-if.
Tom Morrison
 
If Elegance = Simplicity and Terseness while maintaining Understandability (which it is in my book), then Crox's solution above is outstanding! Note that it is almost pseudo-language, a real bonus!
 
Well, while it will work on many COBOL implementations (including RM/COBOL), the Crox solution is not standard conforming due to its use of overlapping operands. (See ANSI COBOL X3.23-1985, Section 6.4.5.) In most situations the standard declares the results of operations on overlapping operands to be undefined. User beware. [neutral]
Tom Morrison
 
Irrespective of whether Crox's solution is standard or not, it does not answer Nagrom's question, which was to remove 'leading spaces' from between fields. Crox's solution will not turn 'How' ' dy' into 'Howdy' unless applied to each separate element of the phrase.

I think Slade's solution of stringing and then unstringing seems the easiest, although that's just a personal opinion.
 
Crox focused on removing leading spaces before concatenation, and therefore can very well lead to the solution. If pulling a donkey doesn't work, try pushing it.

Regards,
Ronald.
 
As being a JSP/COBOL/MVS/JCL/PL1 coach at (ASSYST-)RAET in Holland in '86-'89, we never gave the direct solution to the students. They got clues. We did not take the possibility away for people to think themselves. On this forum we should also people guide to use the manuals, never giving complete solutions, only discussing algorithms and giving clues. For a pro it is more than enough. A student should not get presents here. Study means something!

Regards,

Crox

 
just use move to move 'how' and then use inspect with count to find the location of the first space and use referential (spell?) addressing for the move. If you know the 4th character is blank then you can us move x to y(4:?). For better results use inspect with the reverse intrinsic function to find the first not used character in a field. That way if a field has a word with a space in the middle, they you will not reach it.

example: 'Mc Neal ' If you wanted to write a phrase or a sentence from interactive input of individual words this might be useful. If you do not like my post feel free to point out your opinion or my errors.
 
Here is another solution, which I think will work

DECLARATION PART
----------------
var1, var2 --> of required size
temp-var2 --> same size as that of var2
SPACE-CNT --> 9(number of bytes in var2)

RELEVANT CODE
-------------
INSPECT VAR2 TALLYING SPACE-CNT FOR LEADING SPACES
IF SPACE-CNT > ZERO
MOVE VAR2(SPACE-CNT+1 :) TO TEMP-VAR2
ELSE
MOVE VAR2 TO TEMP-VAR2
END-IF
STRING
VAR1 TEMP-VAR2
DELIMITED BY SPACE
INTO VAR3
END-STRING

Now var3 will contain the required string

Pls try this and give feedback

Thanks
 
Here is another solution, which I think will work

DECLARATION PART
----------------
var1, var2 --> of required size
temp-var2 --> same size as that of var2
SPACE-CNT --> 9(number of bytes in var2)

RELEVANT CODE
-------------

Code:
INSPECT VAR2 TALLYING SPACE-CNT FOR LEADING SPACES
IF SPACE-CNT > ZERO
   MOVE VAR2(SPACE-CNT+1 :) TO TEMP-VAR2
ELSE
   MOVE VAR2                TO TEMP-VAR2
END-IF  
STRING 
     VAR1 TEMP-VAR2 
     DELIMITED BY SPACE 
     INTO VAR3
END-STRING

Now var3 will contain the required string

Pls try this and give feedback

Thanks
 
This is an example

This is a routine I have been using to edit an address.
The street address, City, State, Zip Code are in different fields and the zip code has 2 sub fields. I read the file with a read into and use a copybook for the file layout. i.e. read Personnel-file into ws-personnel-record. I put the address together and insert commas as needed to make a legible address that fits in a form that I then print out.

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top