View previous topic :: View next topic
|
Author |
Message |
sampathkmn
New User
Joined: 12 Dec 2005 Posts: 31 Location: bangalore
|
|
|
|
hi everybody,
i have a doubt:
how to code in a cobol program to calculate the length of the string within a variable?
Eg: 01 A PIC X(80).
and i am passing 40 length string to A(say i am passing any random value)
and i wish to find out the exact length of string stored in variable A. |
|
Back to top |
|
|
i413678 Currently Banned Active User
Joined: 19 Feb 2005 Posts: 112 Location: chennai
|
|
|
|
Hi,
I dont think so there is direct function to find the length of the string....
for this you need to reverse the string ans inspect the reversed string till you find the character...gives the total number of training spaces.
subtract this spaces count from the full length you will get the length of string given by you.....
correct me if i am wrong.....
pavan |
|
Back to top |
|
|
elonics
New User
Joined: 05 Jul 2005 Posts: 49 Location: India
|
|
|
|
Hai friend,
We have a function which tells the length of a string that has been declared. i.e
If WS-STR is declared as
Quote: |
01 WS-STR PIC X(10). |
Then by using the below function we can find the length as 10
Quote: |
DISPLAY LENGTH OF WS-STR |
But you want to know how many non-blank characters a string has then use the following code according to ur requirement
Quote: |
01 WS-LEN PIC S9(04) COMP.
MOVE 'UDAY' TO WS-STR
PERFORM VARYING I FROM 1 BY 1 UNTIL WS-STR(I:1) = ' '
DISPLAY WS-STR(I:1)
IF WS-STR(I:1) NOT EQUAL SPACE
ADD 1 TO WS-LEN
END-IF
END-PERFORM
DISPLAY 'WS-LEN - ' WS-LEN |
This will fetch you WS-LEN as 4. Hope this meets ur requirement.
Thanks
Uday.
---------------------------------------------------------------------
'I may be the best or atleast better than many but never worst' |
|
Back to top |
|
|
i413678 Currently Banned Active User
Joined: 19 Feb 2005 Posts: 112 Location: chennai
|
|
|
|
Hi elonics
thanks for your information
I learned one new thing.......
pavan |
|
Back to top |
|
|
dipanshu
New User
Joined: 16 Jan 2006 Posts: 53 Location: pune
|
|
|
|
Hi,
As I understood, above coding is only meant for single spaces, what if there are multiple spaces?
For example:-
'Abd sher khahn' How to calculate the length for this? |
|
Back to top |
|
|
elonics
New User
Joined: 05 Jul 2005 Posts: 49 Location: India
|
|
|
|
Here is the answer to the above question. It finds how may non blank characters in a string .
Quote: |
01 WS-STR PIC X(30).
01 WS-LEN-1 PIC S9(04) COMP.
01 WS-LEN-2 PIC S9(04) COMP. |
Quote: |
MOVE 'UDAY KIRAN M' TO WS-STR
MOVE LENGTH OF WS-STR TO WS-LEN-2
PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2
DISPLAY WS-STR(I:1)
IF WS-STR(I:1) EQUAL SPACE
ADD 1 TO WS-LEN-1
ELSE
ADD 1 TO WS-LEN-1
END-IF
END-PERFORM
SUBTRACT WS-LEN-1 FROM WS-LEN-2
DISPLAY 'WS-LEN - ' WS-LEN-2. |
This will give you WS-LEN-2 as 10
Udaykiran.M
-----------------------------------------------------------------------
"I am the best or atleast better than many but never worst" |
|
Back to top |
|
|
pa1chandak Currently Banned New User
Joined: 31 Jan 2006 Posts: 55
|
|
|
|
DISPLAY LENGTH OF WS-STR
WILL GIVE YOU THE LENGTH OF FIELD
DO SEND ME THE REPLY
PAWAN C.
09822546416
pa1chandak@yahoo.com |
|
Back to top |
|
|
elonics
New User
Joined: 05 Jul 2005 Posts: 49 Location: India
|
|
|
|
Hai pa1chandak,
"DISPLAY LENGTH OF WS-STR" will give you only length that it is declared with but not that what it actually contains.
El'onics
---------------------------------------------------------------------
"I am the best or atleast better than many but never worst" |
|
Back to top |
|
|
sudheer_kumar
New User
Joined: 27 Dec 2005 Posts: 16
|
|
|
|
Hai pa1chandak,
I analyzed the code. I think your logic is not correct. For reason see below.
IF WS-STR(I:1) EQUAL SPACE
ADD 1 TO WS-LEN-1 ---- > Same
ELSE
ADD 1 TO WS-LEN-1 ---- > Same
END-IF
I think for the above reason we can remove the If condition and just we use
PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2
DISPLAY WS-STR(I:1)
ADD 1 TO WS-LEN-1
END-PERFORM
I ran your code using IBM VISUAL AGE COBOL . The output is a infinite loop. |
|
Back to top |
|
|
elonics
New User
Joined: 05 Jul 2005 Posts: 49 Location: India
|
|
|
|
Hai Sudheer,
I have done one mistake while posting.Sorry for that.The portion which is in else part is commented out in my program.
But my logic is exactly correct.
Usethis
MOVE 'UDAY KIRAN M' TO WS-STR
MOVE LENGTH OF WS-STR TO WS-LEN-2
PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2
IF WS-STR(I:1) EQUAL SPACE
ADD 1 TO WS-LEN-1
END-IF
END-PERFORM
SUBTRACT WS-LEN-1 FROM WS-LEN-2
DISPLAY 'WS-LEN - ' WS-LEN-2.
It will give you ws-len as 10 as i said earlier
El'onics. |
|
Back to top |
|
|
Aji
New User
Joined: 03 Feb 2006 Posts: 53 Location: Mumbai
|
|
|
|
hi
I will give the logic to find the exact length of a variable.
Initialize ws-cnt.
inspect A converting " " to "^".
inspect A tallying ws-cnt for all "^".
compute ws-cnt = length of A - ws-cnt.
inspect A converting "^" to " ".
ws-cnt will give u the exact length of A.
REGARDS
Aji Cherian |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
Sampathkmn,
Quote: |
hi everybody,
i have a doubt:
how to code in a cobol program to calculate the length of the string within a variable?
Eg: 01 A PIC X(80).
and i am passing 40 length string to A(say i am passing any random value)
and i wish to find out the exact length of string stored in variable A.
|
If I move ?Prog to calculate the length of the string within a variable? to A, I count the string as 60, including imbedded spaces. If you count the number of non spaces (not what was asked for) you get 50.
This code will give you the string-len.
Code: |
05 STRING-LEN PIC 9(9) COMP-3.
05 A PIC X(80).
MOVE ?LENGTH OF STING WITHIN A VARIABLE?
TO A.
PERFORM
VARYING STRING-LEN FROM 80 BY -1
UNTIL A(STRING-LEN:1) NOT = ? ?
OR STRING-LEN > 1
END-PERFORM.
|
At this point, the variable string-len will have a value of 33, the length of the string ?LENGTH OF STING WITHIN A VARIABLE?.
Sampathkmn, Please respond with exactly what you were looking for.
Dave |
|
Back to top |
|
|
|