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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL LENGTH OF special register

Status
Not open for further replies.

markmorgan

Programmer
May 13, 2002
19
US
In the COBOL standards, when is/should the LENGTH OF verb (be) evaluated? At run-time or compile time?

The reason I ask is that I am used to using IBM OS/390 and Microfocus COBOL which both evaluate this at run-time and give correct lengths for varying length group levels (OCCURS DEPENDING ON). I am current using Fujitsu Siemens BS2000 COBOL and it appears to be evaluating it at compile time and always giving the maximum length...

It looks like I am going to have to re-code everything using the LENGTH intrinsic function to get arounf this 'feature'.

Mark.
 
Hi,

'length of' should give the true length like IBM and Microfocus and also CA-REALIA do. So the Fujitsu compiler does not generate the right code or at run-time it does not behave the way you expect it to behave.

Why don't you ask Fujitsu about this? What does their manual say about this?

Regards,

Crox

some manual talk hereafter:

Code:
X 1.1.3.3 LENGTH OF





X The LENGTH OF special register contains the number of bytes used by an
X identifier.



X LENGTH OF creates an implicit special register whose content is equal to
X the current byte length of the data item referenced by the identifier.



X Note: For DBCS data items, each character occupies 2 bytes of storage.



X LENGTH OF can be used in the Procedure Division anywhere a numeric data
X item having the same definition as the implied definition of the LENGTH OF
X special register can be used. The LENGTH OF special register has the
X implicit definition:


 X   USAGE IS BINARY PICTURE 9(9)



X If the data item referenced by the identifier contains the GLOBAL clause,
X the LENGTH OF special register is a global data item.



X The LENGTH OF special register can appear within either the starting
X character position or the length expressions of a reference modification
X specification. However, the LENGTH OF special register cannot be applied
X to any operand that is reference-modified.



X The LENGTH OF operand cannot be a function, but the LENGTH OF special
X register is allowed in a function where an integer argument is allowed.



X If the LENGTH OF special register is used as the argument to the LENGTH
X function, the result is always 4, independent of the argument specified
X for LENGTH OF.



X LENGTH OF can not be either of the following:



  X A receiving data item

  X A subscript




X When the LENGTH OF special register is used as a parameter in a CALL
X statement, the parameter must be a BY CONTENT parameter.



X When a table element is specified, the LENGTH OF special register contains
X the length, in bytes, of one occurrence. When referring to a table
X element, it need not be subscripted.



X A value is returned for any identifier whose length can be determined,
X even if the area referenced by the identifier is currently not available
X to the program.



X A separate LENGTH OF special register exists for each identifier
X referenced with the LENGTH OF phrase, for example:


 X   MOVE LENGTH OF A TO B
 X   DISPLAY LENGTH OF A, A
 X   ADD LENGTH OF A TO B
 X   CALL "PROGX" USING BY REFERENCE A BY CONTENT LENGTH OF A



X Note: The number of bytes occupied by a COBOL item is also accessible
X through the intrinsic function LENGTH (see "LENGTH" in topic 7.1.20).
X LENGTH supports nonnumeric literals in addition to data names.
 
Yep, that looks like it is taken form the IBM manual.

Here is the relevant bit from the Siemens Manual. Check out general rule no. 2.

Code:
2.5.11 LENGTH OF

Format

-------------------------------------------------------------------------------
LENGTH OF identifier-1
-------------------------------------------------------------------------------

Syntax rules

1. identifier-1 must not be subject to reference modification.

2. If identifier-1 is a table element then it is not necessary to specify a subscript. If the
subscript is specified then the compiler simply evaluates the format

Comment:
Since the value of the subscript is not relevant for the evaluation of the length, the
compiler simply determines whether the sequence is correct within the expression and
whether identifier-1 is defined. The data type is not checked.

3. identifier-1 must not be defined using the ANY LENGTH clause.

General rules

1. LENGTH OF identifier-1 can be specified at any point in the PROCEDURE DIVISION
where a numeric literal is permitted. This includes subscripts and reference modifiers.

2. LENGTH OF identifier-1 is a constant which is calculated at compilation time.
If identifier-1 is a table with a variable number of elements, then the upper threshold for
the OCCURS clause is used to calculate the constant LENGTH OF identifier-1.

Comment:
Since the length of the data described with the ANY LENGTH clause is not known until
runtime, the only way to determine the length is to use the FUNCTION LENGTH clause.

Who do we think is adhereing to the standards here?

Mark.
 
I vote for IBM, but I guess Stephen J Spiro can help us with the ANS standards.

Regards,

Crox
 
FWIW, the RM/COBOL implementation of the LENGTH OF special register returns the current length of identifier-1. If identifier-1 is a variable length group item or a referenced-modified item (or any other situation where the length cannot be determined at compile time), the length is evaluated at runtime. Tom Morrison
 
The LENGTH special register is not in the 1985 COBOL Standard. Note the 'x' in the left-hand column of the IBM definition: this indicates an EXTENSION in IBM manuals.

The new standard also will not have a LENGTH special register; the LENGTH Function is still available. There will also be an entirely new construct called a CONSTANT, which may be set to the length of a field. It follows the rules for FUNCTION LENGTH, except that it always uses the maximum value for a variable-length data item..

Each compiler maker is free to make its own definition if it chooses to implement the LENGTH special register as an extension.

Stephen J Spiro
ANSI COBOL Standards Committee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top