×
The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.
Am 16.04.2024 um 17:12 schrieb Greg Wilburn <gwilburn@xxxxxxxxxxxxxxxxxxxxxxx>:
I like to use VARCHAR when loading data into data structures that are transformed to JSON for APIs. Both structures are defined with INZ. Does that not really initialize all the subfields?
I hope I can clear this up a bit.
The problem is the varchar data type - lets have a look in detail.
dcl-c myVarChar varchar(50) inz;
The compiler does allocate 52 bytes of (static) memory and it initializes the first two bytes (the length bytes) with a value of x'0000' - meaning binary zero. The remaining 50 bytes of memory are AFAIK not initialized - so you can make no assumptions about the remaining 50 bytes.
myVarChar = 'ABCDE';
Now the compiler sets the memory to 2 bytes with the value x'0005' (binary for length=5) and the next 5 bytes to 'ABCDE'. The remaining bytes are still undefined.
myVarChar = 'ABC';
Here compiler sets the memory to 2 bytes with the value x'0003' (binary for length=3) and the next 3 bytes to 'ABC'. Its very probable that the next two bytes still contain 'DE' - but you can't be sure of that - they are undefined.
Now put such varchar fields into a data structure - they will behave exactly the same.
if myVarChar = 'ABCEDF';
If you compare those varchar directly, the compiler only compares up to the length of the shorter of the two strings - it will never compare the undefined remaining bytes. In fact the compiler may first only compare the length bytes - if those are not equal, the strings can't be equal.
But if you compare two data structures, the whole 52 bytes of a varchar(50) field in both structures will be compared, because they are inside the structures, and a data structure compare always compares the whole length. But you can make no assumptions about the content of the remaining bytes - so even if you assign the exact same values to two separate data structures (whether in arrays or not) you will end up with two different data structures, if the contain varchar fields.
I hope that this may help you. If I wrote something incorrect I would be glad if someone (Barbara? Jon?) may correct me, but I'm pretty sure, that the remaining bytes of the varchar fields are your problem.
HTH and kind regards,
Daniel
As an Amazon Associate we earn from qualifying purchases.