Page 1 of 1

Cobol Calculate the length of a string

PostPosted: Mon Mar 03, 2008 3:23 am
by ignacio
How can I calculate the length of a string if the corresponding intrinsec function is not avaliable?
Is there a better way, in terms of performance, than a PERFORM FROM LEN OF field STEP -1 UNTIL switch-found = True or field(n:1) not = SPACES. I have tried the INSPECT but in some cases does not give a correct result, and in any case is slower than the PERFORM loop option.

Regards.
Ignacio.

Re: Cobol Calculate the length of a string

PostPosted: Mon Mar 03, 2008 6:04 am
by dick scherrer
Hello,

Starting at the "right" side of the field and moving backwards by -1 may be your best bet.

Re: Cobol Calculate the length of a string

PostPosted: Mon Mar 03, 2008 8:33 pm
by ignacio
Thank you Dick, that's what I am doing now.
Regards.
Ignacio.

Re: Cobol Calculate the length of a string

PostPosted: Mon Mar 03, 2008 9:07 pm
by dick scherrer
You're welcome - good luck :)

d

Re: Cobol Calculate the length of a string

PostPosted: Thu Jul 03, 2008 3:01 pm
by mahi
*
PERFORM VARYING IP-NAME-LEN FROM 56 BY -1
UNTIL (IP-NAME-LEN < 1)
OR (IP-NAME-TEXT(IP-NAME-LEN:1)
NOT = SPACE)
END-PERFORM.

Can u please brief out what is this functions.??

Re: Cobol Calculate the length of a string

PostPosted: Thu Jul 03, 2008 10:25 pm
by dick scherrer
Hello mahi and welcome to the forums,

The code you posted will look at the text field and determine the length of the content. The rule for that code is that trailing blanks should not be included in the length.

If you make up some values, move them to the text field and execute that routine with a display of ip-name-len after the perform completes, you will see how it works.