Calculate the length of a string in COBOL.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Aparna V
Registered Member
Posts: 10
Joined: Sun Jan 12, 2014 8:54 pm

Calculate the length of a string in COBOL.

Post 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,
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Calculate the length of a string in COBOL.

Post by zprogrammer »

Did you try Length function with inspect ?
zprogrammer
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Calculate the length of a string in COBOL.

Post 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.
User avatar
Anuj Dhawan
Founder
Posts: 2801
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Calculate the length of a string in COBOL.

Post 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.
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.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Calculate the length of a string in COBOL.

Post 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?
dick scherrer
Former Team Member
Posts: 62
Joined: Wed Aug 07, 2013 6:43 pm

Re: Calculate the length of a string in COBOL.

Post by dick scherrer »

Suggest you post the first output line of a compile showing the release of the cpompiler being used.
Hope this helps,
d
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Calculate the length of a string in COBOL.

Post by zprogrammer »

Hi Anuj,

I did see that already

Just was trying to understand what version of cobol is being used
zprogrammer
User avatar
Anuj Dhawan
Founder
Posts: 2801
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Calculate the length of a string in COBOL.

Post 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.
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.
Aparna V
Registered Member
Posts: 10
Joined: Sun Jan 12, 2014 8:54 pm

Re: Calculate the length of a string in COBOL.

Post 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?"
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Calculate the length of a string in COBOL.

Post 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
zprogrammer
dick scherrer
Former Team Member
Posts: 62
Joined: Wed Aug 07, 2013 6:43 pm

Re: Calculate the length of a string in COBOL.

Post 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.
Hope this helps,
d
Post Reply

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

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”