View previous topic :: View next topic
|
Author |
Message |
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Hi all,
My reqt is to space out the word 'TRAN' if coming at the end of the WS-DATA variable. The word 'TRAN' if coming in the middle of the data should not be spaced out.
01 WS-DATA PIC X(30).
Input
GEORGE HILA AND ROAD LANE TRAN
GEORGE HILA AND TRAN ROAD LANE TRAN
MORROW MOTORS INC TRAN BENZ
STERLING JEEP COMP INC TRAN
Output
GEORGE HILA AND ROAD LANE
GEORGE HILA AND TRAN ROAD LANE
MORROW MOTORS INC TRAN BENZ
STERLING JEEP COMP INC
Code
Code: |
UNSTRING WS-DATA DELIMITED BY 'TRAN' INTO
WS-NAME-FIRST, WS-NAME-SECOND
IF WS-NAME-SECOND = SPACES
INSPECT WS-DATA REPLACING ALL 'TRAN' BY SPACES .
ELSE
CONTINUE
END-IF |
The above code will remove the TRAN if it comes in between words too, which is not correct.
Please guide me.
Thanks
Vinu |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Code a loop to start at the "end" of the data looking for TRAN is the first 4 non-blank characters. When found, move spaces to those bytes.
Suggest you consider Reference Modification. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
thinking |
|
Back to top |
|
|
Mickeydusaor
Active User
Joined: 24 May 2006 Posts: 258 Location: Salem, Oregon
|
|
|
|
What cobol version are you using.??? |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
1. use END-UNSTRING if your are using END-IF,
apparently you are only using one period . per paragraph/section.
2. you have a period in the middle of your IF statement.
3. change your if statement to:
Code: |
IF WS-NAME-SECOND = SPACES
MOVE WS-NAME-FIRST TO WS-DATA
ELSE
MOVE SPACES TO WS-DATA
STRING WS-NAME-FIRST
DELIMITED BY ' ' <<<<that is two spaces
' TRAN '
DELIMITED BY SIZE
WS-NAME-SECOND
DELIMITED BY SIZE
INTO WS-DATA
END-STRING
END-IF
|
when you UNSTRING was finished either - WS-NAME-SECOND = spaces because there was only and 'ending' tran
- WS-NAME-SECOND > SPACES because there was a 'tran' in the middle
regardless, the ending tran will always be dropped, because that is a delimiter and is dropped - regardless.
and don't argue, your code proves this...
Splitting data items (UNSTRING): Delimiters that, when one of them is encountered in the sending field, cause the
current receiving field to stop receiving and the next, if any, to begin receiving
(DELIMITED BY phrase)
so, your error was in not understanding UNSTRING
and the erroneously unnecessary INSPECT in your backasswards IF statement.
Plus the INSPECT statement put a hole of 4 spaces in your data. |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Hi Dick Brenholtz,
Thanks for the code.
The only issue with this is that if 'TRAN' comes in the variable mroe than 2 times, we will get wrong results.
GEORGE HILA TRAN AND TRAN ROAD LANE TRAN
is coming as
GEORGE HILA TRAN AND
So whether reference modification is the only solution ?
Rgds
Vinu |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
No, you could write convoluted code that worked on the array one byte at a time using a subscript/index.
What is the concern about reference modification? |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Hi D.sch,
Yes. I got the desired results with the following code.
Code: |
MOVE FUNCTION REVERSE(WS-DATA) TO WS-DATA-REVERSE
INSPECT WS-DATA-REVERSE TALLYING WS-K FOR LEADING SPACES
COMPUTE WS-NAME-LEN = 30 - WS-K
IF WS-NAME-LEN > 0
IF WS-DATA(WS-NAME-LEN - 4 : 5 ) = ' TRAN'
MOVE WS-DATA(1: WS-NAME-LEN - 5) TO WS-OUTPUT
ELSE
MOVE WS-DATA TO WS-OUTPUT
END-IF
ELSE
MOVE WS-DATA TO WS-OUTPUT
END-IF
|
Thanks
Vinu |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
vinu78 wrote: |
The only issue with this is that if 'TRAN' comes in the variable mroe (sic) than 2 times, we will get wrong results. |
you are correct,
nice solution. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Good to hear it is working. Thank you for letting us know and posting your solution
d |
|
Back to top |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
Another solution could be to use the funtion REVERSE on the character variable, and if the first non-blank characters are 'NART ' (' TRAN' reversed), then space them out, and reverse the string again. |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Thanks Kjeld. I think your solution will be short and good.
I will check that out too. |
|
Back to top |
|
|
|