View previous topic :: View next topic
|
Author |
Message |
smgaru
New User
Joined: 05 Apr 2005 Posts: 5 Location: Hyderabad
|
|
|
|
Hi,
Is there anyway to reverse line of string in COBOL (which length is not known) ??
Please let me know...
Regards
smgaru |
|
Back to top |
|
|
somasundaran_k
Active User
Joined: 03 Jun 2003 Posts: 134
|
|
|
|
Yes.
Try this.
Code: |
WORKING-STORAGE SECTION.
01 YOUR-STRING PIC X(4) VALUE 'ABCD'.
01 REVER-STRING PIC X(4).
PROCEDURE DIVISION.
MOVE FUNCTION REVERSE(YOUR-STRING) TO REVER-STRING
DISPLAY 'REVERSED STRING:' REVER-STRING.
|
hth
-Som |
|
Back to top |
|
|
somasundaran_k
Active User
Joined: 03 Jun 2003 Posts: 134
|
|
|
|
I'm sorry..I didn't read your post completely..
If you do not know the length of the string then you can't use the above solution.
But what do you mean by the length is unknown? There would be a maximum length for this string ,right?
So I assume you have a variable that has been defined to hold the largest string (say PIC X(80)).
Code: |
WORKING-STORAGE SECTION.
01 WS-STRING-1 PIC X(80) VALUE 'MY STRING IS HERE'.
01 WS-REVER-STRING PIC X(80).
01 WS-TALLY PIC S9(04) COMP.
01 WS-LEN PIC S9(04) COMP.
PROCEDURE DIVISION.
INSPECT FUNCTION REVERSE(WS-STRING-1) TALLYING WS-TALLY
FOR LEADING SPACES
COMPUTE WS-LEN = LENGTH OF WS-STRING-1 - WS-TALLY
MOVE FUNCTION REVERSE(WS-STRING-1(1: WS-LEN)) TO
WS-REVER-STRING
DISPLAY 'REVER STRING:' WS-REVER-STRING
|
hth
-Som |
|
Back to top |
|
|
smgaru
New User
Joined: 05 Apr 2005 Posts: 5 Location: Hyderabad
|
|
|
|
Hi Som,
Thanks for showing with code also. But I have one doubt.
Already once we are reversing again why to...
MOVE FUNCTION REVERSE(WS-STRING-1(1: WS-LEN)) TO
WS-REVER-STRING
Instead of the above one will the follwoing wil work??
MOVE WS-STRING-1(WS-TALL: WS-LEN)) TO
WS-REVER-STRING
Pls let me know your clue...
Regards
smgaru |
|
Back to top |
|
|
somasundaran_k
Active User
Joined: 03 Jun 2003 Posts: 134
|
|
|
|
Hi smgaru
The first Reverse function is used to find out the exact length of the string. For finding out the trailing spaces in the string (since the strings can be of variable length,as you mentioned in your initial post), I used reverse with leading spaces.
If your string is having trailing spaces
For eg:
01 WS-STRING-1 PIC X(10) VALUE 'ABCD'.
After the reverse function the string will be
$$$$$$DCBA.($ - Represents spaces). So we need to remove these spaces from the string before reversing it.
Quote: |
You said
Instead of the above one will the follwoing wil work??
MOVE WS-STRING-1(WS-TALL: WS-LEN)) TO
WS-REVER-STRING
|
What is WS-TALL?. I guess its a typo. If you are meaning WS-TALLY
,just wondering how you are going to populate the value for WS-TALLY
hth
-Som |
|
Back to top |
|
|
smgaru
New User
Joined: 05 Apr 2005 Posts: 5 Location: Hyderabad
|
|
|
|
Acually in the first reverse we are getting the string like
$$$$$$DCBA.($ - Represents spaces).
Here the string is already reversed. so again why to use REVERSE function???
Now it is enough to remove the leading spaces.
For extracting the string without leading spaces.
MOVE WS-STRING-1(WS-TALLY+1: WS-LEN)) TO
WS-REVER-STRING
In the above example WS-TALLY value is 6(spaces) and WS-LEN is 4.
Correct me if I am wrong.....
Regards
smgaru |
|
Back to top |
|
|
somasundaran_k
Active User
Joined: 03 Jun 2003 Posts: 134
|
|
|
|
smgaru wrote: |
Acually in the first reverse we are getting the string like
$$$$$$DCBA.($ - Represents spaces).
Here the string is already reversed. so again why to use REVERSE function???
Now it is enough to remove the leading spaces.
For extracting the string without leading spaces.
MOVE WS-STRING-1(WS-TALLY+1: WS-LEN)) TO
WS-REVER-STRING
In the above example WS-TALLY value is 6(spaces) and WS-LEN is 4.
Correct me if I am wrong.....
Regards
smgaru |
The first reverse function I used is for calculating the correct length. Not for reversing the string. This is my First Reverse function.
Code: |
INSPECT FUNCTION REVERSE(WS-STRING-1) TALLYING WS-TALLY
FOR LEADING SPACES
|
Just wondering whether you tried executing this simple code before asking these doubts.
hth
-Som |
|
Back to top |
|
|
|