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

String Problem

Status
Not open for further replies.

gust1480

Programmer
Mar 19, 2002
148
PH
Can somebody please tell me what’s wrong with this code :

WORKING-STORAGE SECTION.
01 TABS.
05 WORD-TAB OCCURS 3 PIC X(40)
.
.
.
.

PROCEDURE DIVISION.

MOVE 1 TO PTR.

STRING WORD-TAB (1) DELIMITED BY ‘ '
‘ ‘ DELIMITED BY SIZE
WORD-TAB (2) DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
WORD-TAB (3) DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
‘PESOS’ DELIMITED BY SIZE
INTO COMPLETE-WORD
WITH POINTER PTR

When I pass some value to WORD-TAB like :
WORD-TAB (1) = ‘ONE HUNDRED THIRTY THREE MILLION’
WORD-TAB (2 = ‘TWO HUNDRED THIRTY ONE THOUSAND’
WORD-TAB (3 = ‘ONE HUNDRED THIRTY THREE’

I always get this kind of result :

ONE HUNDRED THIRTY THREE MILLION TWO HUNDRED THIRTY ONE THOUSAND ONE HUNDRED THIRTY THREE
PESOS

My output always gives the exact size of my table, which is 40, plus the 1 space I added.

Please somebody help me here.
 
Hi Gust,

The problem is that STRING will pad your 40 byte WORD-TABs w/spaces.

What I'd do is replace the spaces between the words in each WORD-TAB with something like '~' (or something that you know won't appear in WORD). Then I'd STRING as follows:
Code:
          PERFORM VARYING PTR FROM 1 BY 1
            UNTIL TABS(PTR:2) = '  '
                  IF TABS(PTR:2) NOT = '  '
                     AND
                     TABS(PTR:1)     = ' '
                     MOVE '~'       TO TABS(PTR:1)
                  END-IF
          END-PERFORM
          STRING WORD-TAB (1) DELIMITED BY ALL ‘ '  
                 ‘ ‘ DELIMITED BY SIZE
                 WORD-TAB (2) DELIMITED BY ALL ‘ ‘
                 ‘ ‘ DELIMITED BY SIZE
                 WORD-TAB (3) DELIMITED BY ALL ‘ ‘
                 ‘ ‘ DELIMITED BY SIZE
                 ‘PESOS’ DELIMITED BY SIZE
          INTO COMPLETE-WORD

          INSPECT COMPLETE-WORD REPLACING ALL '~' BY ' '
HTH, Jack.

 
Hi again Gust,
Agree totally with Jack's solution, but if you fancy a different approach, how about:

WORKING-STORAGE SECTION.
01 TABS OCCURS 3.
05 WORD-TAB PIC X(40).
05 WORD-LEN PIC S9(4) COMP.
01 WS-SUB PIC S9(4) COMP.

PEFORM
VARYING WS-SUB FROM 1 BY 1
UNTIL WS-SUB > 3
PERFORM
VARYING WORD-LEN(WS-SUB) FROM 40 BY -1
UNTIL WORD-LEN(WS-SUB) = 0
OR WORD-TAB(WORD-LEN(WS-SUB):1)(WS-SUB) NOT = ' '
END-PERFORM
END-PERFORM

STRING WORD-TAB(1:WORD-LEN(1))(1)
' '
WORD-TAB(1:WORD-LEN(2))(2)
' '
WORD-TAB(1:WORD-LEN(3))(3)
INTO COMPLETE-WORD

I've not checked the syntax of this as to where the relevant subscripts go as I seem to recall that it can be a little strange. What the code is trying to do is find the last non blank in each word, and string the lot together by lengths.

HTH
Marc
 
Your code SHOULD work. However, I notice that your "space to be printed" is (in your example) '', not ' '. I think you are confusing the parser, and it is looking for a whole bunch of spaces as the delimiter, or spaces with apostrophes. Try two things:
Replace the first ' ' with SPACE
Make sure the separator is ' ' and not ''.

Stephen J Spiro

Incidentally, unless you actually intend to use the pointer, it is optional in the statement.
 
Thanks guys for the tips. By the way Stephen, I did made sure that the ' ' is written the write way. And it still gives me the same result.
And Slade, why should I have to change the spaces to a different delimeter, does that mean when I change it it will look like this :
ONE*HUNDRED*THIRTY*THREE*MILLION***********************’
I don't see any difference if I just ask for the spaces.
I hope you won't get offended by this. It's just that I want to be sure with the answer because my compilation is limited.
 
Hi Gust,

What I suggested would return:

'one*hundred*thirty*three*million '

after the PERFORM.

I'm not sure if the STRING using DELIMITED BY ALL ' ' would ignore the spaces between the words and string the entire phrase into the receiving field, so I used the perform to change the single spaces to '~'. Now, the "delim by all" will do what you want: string all WORD-TABs with a single space separating them. After the STRING,
COMPLETE-WORD will contain:

"ONE~HUNDRED~THIRTY~THREE~MILLION "
not:
"ONE~HUNDRED~THIRTY~THREE~MILLION~~~~~~~~~~~~~~~~~~~~"

The stmt "IF TABS(PTR:2) NOT = ' '" prevents the trailing spaces from being replaced by "~"s.

After the INSPECT it will contain:

"ONE HUNDRED THIRTY THREE MILLION "

If the "delim all ' '" will ignore the single spaces, that's even better. I don't have a platform to run a test. Why don't you try it? Just change your original code to add "ALL" to each WORD-TAB delim. See if it works. If it does, it's a lot simpler than what I have; if not, mine'll work.

Regards, Jack.

 
Hi Gust,

What I suggested would return:

'one*hundred*thirty*three*million '

after the PERFORM.

I'm not sure if the STRING using DELIMITED BY ALL ' ' would ignore the spaces between the words and string the entire phrase into the receiving field, so I used the perform to change the single spaces to '~'. Now, the "delim by all" will do what you want: string all WORD-TABs with a single space separating them. After the STRING, COMPLETE-WORD will contain:

"ONE~HUNDRED~THIRTY~THREE~MILLION "
not:
"ONE~HUNDRED~THIRTY~THREE~MILLION~~~~~~~~~~~~~~~~~~~~"

The stmt "IF TABS(PTR:2) NOT = ' '" prevents the trailing spaces from being replaced by "~"s. BTW, that's 2 spaces between the single quotes.

After the INSPECT it will contain:

"ONE HUNDRED THIRTY THREE MILLION "

If the "delim all ' '" will ignore the single spaces, that's even better. I don't have a platform to run a test. Why don't you try it? Just change your original code to add "ALL" to each WORD-TAB delim. See if it works. If it does, it's a lot simpler than what I have; if not, mine'll work.

Regards, Jack.

 
Hello Jack,
I'm really sorry if I'm being a pest here, but, I used double space ' ' to delimit my WORD-TAB (1), (2) & (3) so that it would not separate each word. Cause I only want the word 'ONE HUNDRED THIRTY THREE MILLION’ without the trailing spaces so that I can STRING it with (2) & (3).
And have a normal output like this:
ONE HUNDRED THIRTY THREE MILLION TWO HUNDRED THIRTY ONE THOUSANDONE HUNDRED THIRTY THREE PESOS
I hope you would understand, I'm new at Cobol so I don't know hoe to use IF TABS(PTR:2) NOT = ' '
 
Hi Gust,

No, you're not being a pest, that's what this forum is for: to get answers. Sometimes you do, sometimes you don't, sometimes you get the wrong answer. :)

Now that I see you delimited with 2 spaces, I see your confusion. It looks like the pointer (explicit or implied) should not have advanced beyond the position it was at when the last non space in each sending field was encountered. This should give the desired result.

Why isn't it? Beats me! Maybe your compiler's implementation? Maybe a compiler bug? Maybe our misunderstanding of how the feature works? Who knows?

There's an old saying that goes "Pay the 2 pesos". OK, maybe it was "2 dollars", but you get the point. How to get it to work?

BTW, the (PTR:2) part of my code is called reference modification. Take a look thru your manual for all the deails, but briefly, it allows you to access any part of a field (i.e. a subtring). PTR contains the position # in the field; 2 indicates the length of the segment in the field. You could use a variable for that too. Testing for
TABS(PTR:2) says, in effect, if it's space, but it's a trailing space, don't change it to ~.

HTH, Jack.

 
Hi Jack,
I finally got it figured out. I had problem unstringing the WORD-TAB with double spaces is because I didn't initialize the WORD-TAB to spaces before I used it.
Again, I want to thank you very very very much for being patient with me.


Jimmy
 
Hi Jimmy,

Ugh (heel of hand to forehead), of course!

Good luck, Jack.
 
Something does not compute. Unless you used STRING to fill each WORD-TAB, initializing to spaces should make no difference: a MOVE would result in padding the field with SPACES, even if they overlaid existing spaces.

Stephen J Spiro
 
Stephen,

Good point, unless he used ref mod or some crazy redefines.

Jack
 
Stephen,
Thanks for the info but I have tried it already and it works.

Jimmy
 
Hi Gust,

It may be that both you and Stephen are correct. How DO you populate the WORD-TAG table? That may explain it.

Regards, Jack.
 
Hi Jack,
Well, I actually used a UNSTRING to fill the WORD-TAB. So, Stephen is right. Since I used the UNSTRING, moving spaces to WORD-TAB worked.

Jimmy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top