Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Prog to calculate the length of the string within a variable

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Prog to calculate the length of the string within a variable
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    Post subject:
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    Post subject: Re: string length count
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    Post subject:
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    Post subject: Query-If multiple spaces
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    Post subject: Re: Prog to calculate the length of the string within a vari
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    Post subject:
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    Post subject: Re: Prog to calculate the length of the string within a vari
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    Post subject:
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    Post subject: Re: Prog to calculate the length of the string within a vari
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    Post subject:
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    Post subject: Re: Prog to calculate the length of the string within a vari
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts SQZ pipe delimited file with 1 column... Rick Silvers DFSORT/ICETOOL 2 Sun Nov 06, 2016 8:11 pm
No new posts Joinkeys operation when key1 and key2... Susanta DFSORT/ICETOOL 5 Wed Nov 02, 2016 5:35 pm
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Format to pipe delimited with variabl... pshongal SYNCSORT 6 Wed Sep 14, 2016 2:48 pm
No new posts How to calculate an specific transact... lind sh CICS 2 Tue Sep 13, 2016 9:20 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us