01 variableLengthItem any length pic x.
allocate 50 charaters returning myPointer.
set address of variableLengthItem to myPointer.
Above construct still makes only available the first byte, since the length is not adjusted in the cobol field.
According to MF docs, even a move should adjust the length:
move "abcde" to variableLengthItem.
which should adjust the length of the item to 5.
Why needed?
My system has variable text, say dutch, english and german variations. A function will retrieve the text, which has variable length of course. The result must be unstringed and restringed again to obtain normalised displaying of these text. The unstring (and re-string) should be totally obsolete.
Hi Wim,
the
ANY LENGTHclause does not imply that an item's size may be varied within the program/function: the length of anANY LENGTHis item is the same as the corresponding argument passed to the program/function.The feature you're requesting is
DYNAMIC LENGTH, which was added in COBOL 2014 and allows for the more flexible string-handling found in modern languages.I see. Didn't know about DYNAMIC LENGTH.
This does mean that creating a variable length string in a function is not possible until DYNAMIC LENGTH is available, I guess? Of course using odo is alternative, though not really elegant.
Correct.
Is there anything stopping you from using a
PIC X(500), say, and trimming stuff as necessary? It'll be no different from an ODO which is also of fixed size (despite pretending not to be).Yes. There is no real limitation: the variable can be length zero, can be any length.
Than, working with x(500) or, say, x(21634) is ugly and breathes amateurism.
The only choice is wait for it, leave development or use a c routine emulating a function.
This may fire back, I know.
One of the nice parts about alphanumeric "string" functions is the variable length.
GC should allow setting the length for
ANY LENGTHreturning items in UDF, but I have not seen a clear documentation (including in the standard) how to set this.This looks like a good reason for Brian to ask his comitee contacts.
There's no documentation because the standard doesn't allow it:
The length of this
PICTUREclause is fixed while in the function, so the length of theANY LENGTHitem cannot be varied from within the function. OnlyDYNAMIC LENGTHlets you freely vary the length of item.The OO variant of MF defines a PROPERTY clause with SET and GET
PROPERTY clause
and a SET syntax
SET satement
and a syntax for RETURNING in the PROCEDURE DIVISION header, see Format-3 in PROCEDURE HEADER
Well, these options come from various compilers, and I'm not sure if it can be found in the standard. Now on GC:
The ANY LENGTH clause is intended for defining USING paramaters and the item will
inheritthe length of the caller's item. Though it is possible to define such item as a RETURNING item without any reference to a USING item; at least the compiler has no problems. GC allocates for this ittem 1 byte bycob_malloc(1U), which is arbitrary (a zero length would just do). This state-of-the-art is pretty useless and a simplereallocon the very first reference would do the job. It is impossible to adjust the length of such item using a called c-subroutine; that MUST be a emulated function. This raises the problem that the item with adjusted length cannot be stored, since the function returns a temporary item.Having said all this, one should realize the Cobol is about fixed length data; so a solution for returning variable length items interferes with Cobol's architecture. OO variants however implemented it easily.
Last edit: wim niemans 2016-02-19
That
SETstatement andRETURNING ... AS ...is part of MF's Managed COBOL dialect, which appears to be a blatant attempt at vendor lock-in.I'm not sure what you're referring to here.
The udf:
The result is that stringy has a size of 1. It is impossible to change that size.
Last edit: wim niemans 2016-02-19
The docs you've referred to neither apply to a real COBOL environment but to MF'S .NET/JVM compilers (and therefore does not even count as "make migration easier"), nor to
ANY LENGTHitems.It's very unlikely that GnuCOBOL will support these as an extension.
Hm, some additional quotes from the standard:
and
And now the main part:
Conclusion (maybe confusion):
If a function
RETURNINGanANY LENGTHitem is used, it must have an returning item. Therefore it's not useable inSTRING,DISPLAYor other statements like this as these don't have a returning item. The only possible statement I can think of isMOVE function..., maybeWRITE something FROM function....In this case a
MOVEwould set the size for theANY LENGTHitem, but in the calling program.The runtime would have to check for this and set the length accordingly. For example:
The expected result from the quoted specs would be:
but the result is:
as the returning item is allocated with one byte and never changed.
Is this a valid conclusion or only a confusion? Any thoughts or maybe even knowledge?
Simon
BTW: I'd still like to have a statement from the standard comitee.
Last edit: Simon Sobisch 2016-02-19
Functions aren't allowed to return
ANY LENGTHitems (§ 13.8.2.2.4):Last edit: Edward Hart 2016-02-20
I don't know from where you're quoting this, and I am not aware of this limitation.
Besides that the compiler gladly accepts the construct.
I'm also quoting from the 2014 COBOL standard.
The compiler is wrong ;-). I'll start fixing this now.
This is true, I was confused by the part "ANY LENGTH may be used [...] as RETURNING item" in the standard, but this only applies to methods (which aren't available in GnuCOBOL).
Ed tracked and fixed the compiler error with [bugs:#203], therefore I'll close this ticket.
Related
Bugs:
#203Yes, this is exactly what I figured out. There are 2 aspects:
Which means that functions written in Cobol cannot return variable length items, which makes it impossible to use string manipulation where the string length is varying (expanding string), like
SUBSTITUTE '{tplVar}' IM stringx WITH 'John Doe, Washington'.My conclusion is that this can only be implemented using intrinsic functions or system supplied subroutines, since
concatenate(A, B)does the job marvellous.I likely would have answered the same as Ed. But with a question added:
Pleaes link the actual doc you refer to and we see what implementation is possible. Thank you.
Simon
I am very sure I've read about that. The specific doc however cannot be found again; so leave it as not being written. Sorry.
Found it again, though it was NOT at MF, but from a discussion at ISO/IEC/JTC 1/SC 22/WG 4 in 2008. I must admit that I don't know about the status of this change (if it reached the standard or was redesigned as DYNAMIC LENGTH or was simply ignored).
05 notes PIC X ANY LENGTH.
If the item has a maximum length (n), ANY LENGTH LIMIT n is written or, if there is no theoretical maximum length, ANY LENGTH is sufficient.
Any-length items may be national items, or bit items:
05 city-name PIC 1 ANY LENGTH.
05 customer-name PIC N ANY LENGTH.
When a new value is stored in an any-length elementary item, the item's length is automatically adjusted, subject to any maximum length. For example,
MOVE "This product is no longer available" TO notes
sets the length of notes to 35. To find this length, you use
FUNCTION LENGTH (notes)
Until its value changes again, this data item will now behave exactly like a fixed-length item with a PICTURE of X(35), and all the statements normally available to an alphanumeric or national data item, such as MOVE, STRING, UNSTRING, INSPECT, ACCEPT, DISPLAY can be used with it.
Either of the following two statements
INITIALIZE notes or MOVE SPACES TO notes
reduces the item to a zero length data item.
Last edit: wim niemans 2016-02-20
Thank you for the reference. I did not found this discussion but I have not checked everything at http://www.cobolstandard.info/wg4/document.html - maybe it's in there.
It was good to take the time to research it: it was in before and there are different references (most for "upcoming 2008 standard") for this syntax available.
Ed was correct again. The "dynamic" ANY LENGTH is included in the 2014 standard but as
DYNAMIC LENGTH. For more information and the new ticket see [feature-requests:#109].It's funny to see that there are even books out (in this case written in German) declaring the old version with the "dynamic"
ANY LENGTHto be part of the COBOL 2014 standard. But this book says that "COBOL has full XML support now, as it includes ISO/IEC TR 24716: 2007 completely", too...Related
Wish List: #109
Thank you for reporting the compiler bug ;-)
ANY LENGTHas returning item should not be possible at all but marked as an error. Therefore I'm closing this ticket as invalid.BTW: The compiler bug was fixed by Ed in [r775].
Please note the following statement from ISO/IEC 1989:200x WD 1.10, on 8.5.1.7 Any-length elementary items
There is nothing in the document that prohibits a returng item of ANY LENGTH. But okay, it is not the last document on 2014 standard.