IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Prog to calculate the length of the string within a variable


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sampathkmn
Warnings : 1

New User


Joined: 12 Dec 2005
Posts: 31
Location: bangalore

PostPosted: Tue Jan 31, 2006 3:08 pm
Reply with 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.
Back to top
View user's profile Send private message
i413678
Currently Banned

Active User


Joined: 19 Feb 2005
Posts: 112
Location: chennai

PostPosted: Tue Jan 31, 2006 3:26 pm
Reply with quote

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
View user's profile Send private message
elonics

New User


Joined: 05 Jul 2005
Posts: 49
Location: India

PostPosted: Tue Jan 31, 2006 3:55 pm
Reply with quote

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
View user's profile Send private message
i413678
Currently Banned

Active User


Joined: 19 Feb 2005
Posts: 112
Location: chennai

PostPosted: Tue Jan 31, 2006 4:24 pm
Reply with quote

Hi elonics


thanks for your information

I learned one new thing.......

pavan
Back to top
View user's profile Send private message
dipanshu

New User


Joined: 16 Jan 2006
Posts: 53
Location: pune

PostPosted: Wed Feb 01, 2006 10:43 am
Reply with quote

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
View user's profile Send private message
elonics

New User


Joined: 05 Jul 2005
Posts: 49
Location: India

PostPosted: Wed Feb 01, 2006 4:09 pm
Reply with quote

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
View user's profile Send private message
pa1chandak
Currently Banned

New User


Joined: 31 Jan 2006
Posts: 55

PostPosted: Wed Feb 01, 2006 5:36 pm
Reply with quote

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
View user's profile Send private message
elonics

New User


Joined: 05 Jul 2005
Posts: 49
Location: India

PostPosted: Thu Feb 02, 2006 9:41 am
Reply with quote

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
View user's profile Send private message
sudheer_kumar

New User


Joined: 27 Dec 2005
Posts: 16

PostPosted: Thu Feb 02, 2006 3:11 pm
Reply with quote

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
View user's profile Send private message
elonics

New User


Joined: 05 Jul 2005
Posts: 49
Location: India

PostPosted: Thu Feb 02, 2006 3:51 pm
Reply with quote

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
View user's profile Send private message
Aji

New User


Joined: 03 Feb 2006
Posts: 53
Location: Mumbai

PostPosted: Fri Feb 03, 2006 3:45 pm
Reply with quote

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
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Sat Feb 04, 2006 3:04 am
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Store the data for fixed length COBOL Programming 1
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
Search our Forums:

Back to Top