Calculate the length of a string in COBOL.
Calculate the length of a string in COBOL.
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,
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,
-
- Global Moderator
- Posts: 588
- Joined: Wed Nov 20, 2013 11:53 am
- Location: Mars
Re: Calculate the length of a string in COBOL.
Did you try Length function with inspect ?
zprogrammer
- Robert Sample
- Global Moderator
- Posts: 1903
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: Calculate the length of a string in COBOL.
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.
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.
- Anuj Dhawan
- Founder
- Posts: 2824
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: Calculate the length of a string in COBOL.
Aparna V wrote:If the intrinsic functions are not available - what is the better way to calculate the length of a string?
OP does not want to use intrinsic functions.Pandora-Box wrote:Did you try Length function with inspect ?
Thanks,
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
- Robert Sample
- Global Moderator
- Posts: 1903
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: Calculate the length of a string in COBOL.
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?If the intrinsic functions are not avaliable - what is the better way to calculate the length of a string?
You could write your own reverse subprogram to replace the intrinsic function -- but why duplicate what the compiler allows you to use?
-
- Former Team Member
- Posts: 62
- Joined: Wed Aug 07, 2013 6:43 pm
Re: Calculate the length of a string in COBOL.
Suggest you post the first output line of a compile showing the release of the cpompiler being used.
Hope this helps,
d
d
-
- Global Moderator
- Posts: 588
- Joined: Wed Nov 20, 2013 11:53 am
- Location: Mars
Re: Calculate the length of a string in COBOL.
Hi Anuj,
I did see that already
Just was trying to understand what version of cobol is being used
I did see that already
Just was trying to understand what version of cobol is being used
zprogrammer
- Anuj Dhawan
- Founder
- Posts: 2824
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: Calculate the length of a string in COBOL.
Sorry Pk, I kept the thread opened long enough and did not see that there were other replies as well to the thread...
. If sounded like raising eye-brows, that is not what I meant.

Thanks,
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Re: Calculate the length of a string in COBOL.
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?"
"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?"
-
- Global Moderator
- Posts: 588
- Joined: Wed Nov 20, 2013 11:53 am
- Location: Mars
Re: Calculate the length of a string in COBOL.
Anuj,
Np..Don't feel sorry .. We are good "e-buddies"
Aparna,
If it is for the learning exercise :
First try using :
Perform Until alone
Np..Don't feel sorry .. We are good "e-buddies"

Aparna,
If it is for the learning exercise :
First try using :
Perform Until alone
zprogrammer
-
- Former Team Member
- Posts: 62
- Joined: Wed Aug 07, 2013 6:43 pm
Re: Calculate the length of a string in COBOL.
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.
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.
As mentioned earlier, COBOL does not have strings as they are understood in other languages.
There cannot be good it the code does not work 100% of the time. Speed does not matter for getting an incorrect answer.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?"
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.
Hope this helps,
d
d
Create an account or sign in to join the discussion
You need to be a member in order to post a reply
Create an account
Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute