Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To find the length of a string... 1

Status
Not open for further replies.

ashykhanna

Programmer
May 27, 2002
14
IN
hello,
can anyone let me know how to find the length of a given string using cobol.
suppose,

10 ws-data pic x(64)

i move some string to this variable.i would like to find the length of the string moved to ws-data.
its very urgent.
thank you,
Ashok..
 
Hi Ashok,

Under OpenVMS, we have to use a system call to determine the actual number of positions occupied (SYS$TRIM).
I'll be interested myself to see what others come up with.
What platform/version are you using?

[ponder]
 
* RETURN LENGTH OF WS-DATA IN WS-DATA-LENGTH
COMPUTE-WS-DATA-LENGTH SECTION.
CWDL01.
IF WS-DATA = SPACES
MOVE 0 TO WS-DATA-LENGTH
GO TO CWLD99.
MOVE LENGTH OF WS-DATA TO WS-DATA-LENGTH.
CWLD10.
IF WS-DATA(WS-DATA-LENGTH:1) = SPACE
SUBTRACT 1 FROM WS-DATA-LENGTH
GO TO CWLD10.
CWLD99.
EXIT.
 
MOVE WS-MY-STRING TO WS-DATA
COMPUTE WS-MOVE-LENGTH =
FUNCTION LENGTH(WS-MY-STRING)


PERFORM VARYING WS-MOVE-LENGTH
FROM WS-MOVE-LENGTH BY -1
UNTIL WS-MOVE-AREA(WS-MOVE-LENGTH:1) NOT = ' '
OR WS-MOVE-LENGTH = 0
END-PERFORM.


This should do the trick, without using any GOTO statements. Just be careful WS-MY-STRING is never longer than the 64 or you'll abend. To safegaurd this you could always start off with WS-MOVE-LENGTH set to 64 instead of using the function call, or use the function call against WS-DATA, and you wouldn't have to worry about the length of the "sending field".

I think the important thing for you to grasp is the fact that you can code it easily with the PERFORM.

Either way. WS-MOVE-LENGTH will give you the length of the string.
 
A minor, but important, correction:

The order of the terms in your UNTIL phrase is very important. See faq209-1604.

In this particular case, when WS-MOVE-AREA contains spaces, on the final iteration of the PERFORM you will be evaluating WS-MOVE-AREA(0:1) which should cause the program to terminate. By switching the order of the terms you will cause WS-MOVE-LENGTH = 0 to be evaluated first, thereby eliminating the test of WS-MOVE-AREA(0:1). Tom Morrison
 
Tom,

thanks for pointing that out. I must admit it is something I was aware of, but my full code wouldn't have got to there.

In the program which I lifted this out of, due to efficiency reasons I didn't even perform this bit of code if my sending field was spaces. Hence I wouldn't hit the condition, you mention. Perhaps I should have posted more of my source code. I must admit however, I would do as you have advised from now on as its much safer from a maintenance point of view.

The main aim of my post was to demonstrate not using a GOTO. Still thanks once again for the extra info. As I said, I was aware of the (0:1) situation and didn't call my section of code if the sending field is spaces, so it didn't happen for me. However I must admit I wasn't aware that the order of the terms was important and feel a Star is in order for that info.

Thanks
Greg
 
What is wrong with GO TO if it is used for small loops?
Of course I know, if GO TO is used to jump between sections or if half of the statements is a GO TO, going forwards, backwards and forwards again, a program can become unreadable and hard to maintain. But for small loops like this I think a GO TO is better readable and the same good to maintain.
Desparately trying to find alternatives can lead to errors and mistakes as well, as you can see above.

Marcel

 
Thanks, Greg, for the star! [2thumbsup]

Marcel, go to-less programming is sometimes a religious issue, sometimes a matter of a specific organization's coding standards. From a compiler's point of view, it is easier to optimize loops that do not contain go to. I remain neutral on the issue, and try to do whatever makes the most maintainable code, since I will probably end up being the programmer doing the maintenance! Tom Morrison
 
Marcel,

I wasn't picking at your code. I was merely trying to show an alternative way, without using GO TO. I feel I accomplished that. I wasn't saying there was anything wrong or anything right with your solution. Simply trying to give Ashok a choice.

I'm not wishing to become embroiled in a slanging match, but as I pointed out, in the program I lifted it from, I didn't actually perform this generic section if the sending field was equal to spaces. Therefore there was no error in my code, I thought I'd made that clear.

A little bit like your statement

IF WS-DATA = SPACES
MOVE 0 TO WS-DATA-LENGTH
GO TO CWLD99.

only I had my code "wrapped" in a perform.

I was performing this code for many different fields on a table from a database and I only performed the code if the field being processed wasn't equal to spaces. Hence the entry

MOVE WS-MY-STRING TO WS-DATA

wasn't actually in my program and the error condition mentioned could not have been hit in the solution I coded. Although I do think Tom made a great point. I think the main thing is that Ashok has a solution(or 2) don't you?

Cheers
Greg




 
Greg,

I agree with you completely, and I am not angry with anybody. Also I think Tom made a good point writing about the order of the terms. Also i can agree with Tom when he writes it is an almost religious issue.

The issue is this: I am a simple man liking simple statements. If I see a statement I want to see right away what it is doing without needing to think about it. So I use simple statements without all the optional features. If that means I need more lines of code to get something done, so be it.

In the past I have ported many cobol code among different compilers and operating systems. It is my experience, the simple statements rarely cause trouble when porting, but the complicated do sometimes. Fortunately, all compilers and operating systems are made by humans and contain bugs, otherwise it would be very boring ...

Marcel
 
Hi,

If the dialect of COBOL allows it then a simple:
UNSTRING xyz DELIMITED BY SPACES
WITH POINTER i...
will suffice.

Regards,

Mike.
 
Mike,

can you expand on this further please with a full code example. This isn't a way I was aware you could do it and looks really useful. Does it give you the desired result for instance with a variable containing "GREG SIMPSON".

Appreciate it if you could provide further information.

Thanks
Greg
 
Hi,

The situation is more difficult where you must allow for multiple tokens in the input string - this situation may be better catered for by using the loop with a decrementing index.

However, the UNSTRING statement would be...

INITIALIZE p.
UNSTRING xyz DELIMITED BY SPACES
INTO a b c
WITH POINTER p.

p would then contain the number of characters 'consumed' from xyz.

Regards,

Mike.
 
Mike,

Let's think about this a second...
Code:
INITIALIZE p
Given that p is numeric, p will contain zero (0) after the execution of this statement. This will cause
Code:
UNSTRING ... WITH POINTER p
to have an overflow condition.

If p is initialized to one (1), then, after the execution of the UNSTRING, the value of p will be the number of characters consumed plus 1. However, the number of characters consumed will include the terminating delimiter (SPACES), thus the relationship of the value of p to the desired result is not clear.

And, if the value of the string contains more than three tokens, the UNSTRING fails to give the correct result.

One may use UNSTRING in a PERFORM loop to achieve the correct result but, absent some really good reason to do so, there seems to be little reason to use UNSTRING in this rather obscure manner. Tom Morrison
 
hii, this is Ashok.
Thank u all for ur valuable answers,comments and examples.
i work on OS/390.
About the GO TO problem:
Mkuiper, actually my organisation follows certain coding standards,which does not recommend the usage of GO TO.there's nothing wrong with the code as such.

Thank U ALL.
Ashok.
 
why hot use inspect with a counter in conjunction with function reverse(variable). That way the inspect command will tell you the number of spaces at the end of the field from the reverse direction.

Confused?

This will count the number of spaces on the end of a text variable regardless of whether there is a space in the center of your variable or not.

For instance if you have a variable that is 10 characters and contains "HI THERE" the result is 2. 10-2=8 (length of string). Note this is the number of characters in the string minus a terminator that you may see in an ASCII PC field. If you do not like my post feel free to point out your opinion or my errors.
 
Hi Ashok,

If your compiler supports it you can try:
Code:
  inspect reverse(ws-data) tallying ws-t-spaces
          for leading spaces 
  compute ws-len-of-string = length of ws-data 
                           - ws-t-spaces
I think that will work.

Regards, Jack.
 
Sorry Ceh, didn't read your post. I was scanning for code with reverse/inspect. Didn't see your text.

Well now we have a coding example to go with your dissertation. :)

Regards, Jack.
 
hi,
use this code....and reap the result...

Identification Division.
Program-ID. "TESTPIC".
Author . thiyagarajan.
DATE-WRITTEN. AUGUST 09, 2002.
Data Division.
Working-Storage Section.
77 TEST-DATA PIC X(100).
77 f-DATA PIC X(100).
77 I PIC X(100).
77 t-count PIC 9(2).
Procedure Division.

000-MAIN-PARA.
DISPLAY "ENTER SOME VALUE FOR THE TEST-DATA".
ACCEPT TEST-DATA.
string test-data, "." delimited by " " into f-data.
PERFORM 111-PROCESS-PARA.

STOP RUN.

111-PROCESS-PARA.


Inspect f-data TALLYING t-count for characters
before initial ".".
display "string(l)" t-count.

bye,
thiyagu
 
I use to use function reverse(varaiable) with the inspect verb and a counter to count the spaces on the end of the varaible and Just subtract to get the size.

INSPECT FUNCTION REVERSE(WS-ADDRESS-1)
TALLYING BLK-CTR FOR LEADING SPACES. If you do not like my post feel free to point out your opinion or my errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top