Page 1 of 1

Calculate the length of a string in COBOL.

Posted: Thu Jan 16, 2014 3:54 pm
by Aparna V
Hi,

If the intrinsic functions are not avaliable - what is the better way to calculate the length of a string?

I've tried using PERFORM FROM length OF field STEP -1 UNTIL some-switch = True or field(n:1) not = SPACES. I've also tried the INSPECT but in some cases it does not give correct result, and in any case is slower than the PERFORM loop option - so which one is good?

Regards,

Re: Calculate the length of a string in COBOL.

Posted: Thu Jan 16, 2014 4:20 pm
by zprogrammer
Did you try Length function with inspect ?

Re: Calculate the length of a string in COBOL.

Posted: Thu Jan 16, 2014 7:32 pm
by Robert Sample
Technically, COBOL does not have strings as they are typically defined in other languages since COBOL variables have their length known at compile time (with only a couple of exceptions). And if you are talking about WORKING-STORAGE variables, then they are not "strings" -- period -- as their length is set at compile time.

Assuming that you are wanting to find the last non-blank character in an alphanumeric variable, use the REVERSE intrinsic function and then use INSPECT TALLYING LEADING SPACES on the reversed variable.

Also, with current machines, the ONLY time you should worry about performance is if your application is not completing in its window. Worrying about performance issues, especially without a known issue, is pretty close to useless. Modern machines execute tens of millions to billions of lines of COBOL for every second of CPU time. So unless your code is being executed, literally, BILLIONS of times -- you almost certainly will NOT be able to detect the CPU difference between methods. If you use SMF data and get CPU time to millionths of a second, then you might be able to detect the difference -- if your code gets executed enough times. So you have already spent more time worrying about the performance than could possibly be saved in the next many years of execution.

Re: Calculate the length of a string in COBOL.

Posted: Thu Jan 16, 2014 7:47 pm
by Anuj Dhawan
Aparna V wrote:If the intrinsic functions are not available - what is the better way to calculate the length of a string?
Pandora-Box wrote:Did you try Length function with inspect ?
OP does not want to use intrinsic functions.

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 12:55 am
by Robert Sample
If the intrinsic functions are not avaliable - what is the better way to calculate the length of a string?
Considering how long intrinsic functions have been available in IBM COBOL, why on earth would you even ask this question? If you are dealing with a COBOL other than mainframe IBM COBOL, intrinsic functions may not be there but if so why ask on a mainframe forum?

You could write your own reverse subprogram to replace the intrinsic function -- but why duplicate what the compiler allows you to use?

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 12:58 am
by dick scherrer
Suggest you post the first output line of a compile showing the release of the cpompiler being used.

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 10:20 am
by zprogrammer
Hi Anuj,

I did see that already

Just was trying to understand what version of cobol is being used

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 2:45 pm
by Anuj Dhawan
Sorry Pk, I kept the thread opened long enough and did not see that there were other replies as well to the thread...:oops:. If sounded like raising eye-brows, that is not what I meant.

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 2:57 pm
by Aparna V
We're using Enterprise COBOL, however this is not a prod problem, it's a learning exercise for us new programmers rather. That's why I said this in start:

"I've tried using PERFORM FROM length OF field STEP -1 UNTIL some-switch = True or field(n:1) not = SPACES. I've also tried the INSPECT but in some cases it does not give correct result, and in any case is slower than the PERFORM loop option - so which one is good?"

Re: Calculate the length of a string in COBOL.

Posted: Fri Jan 17, 2014 3:49 pm
by zprogrammer
Anuj,

Np..Don't feel sorry .. We are good "e-buddies" :D

Aparna,

If it is for the learning exercise :

First try using :

Perform Until alone

Re: Calculate the length of a string in COBOL.

Posted: Sat Jan 18, 2014 12:53 am
by dick scherrer
In addition to working on a "solution", i strongly recommend your organization use standard terminology - not just something that group understands.

As mentioned earlier, COBOL does not have strings as they are understood in other languages.
I've also tried the INSPECT but in some cases it does not give correct result, and in any case is slower than the PERFORM loop option - so which one is good?"
There cannot be good it the code does not work 100% of the time. Speed does not matter for getting an incorrect answer.

In which case(s) does the code give incorrect results? For us to be much help, you need to show what you are trying, a bit of sample input, and the results given for the run. The sample input should contain a couple that work and a coule that do not.