On Thu, Apr 15, 2021 at 3:09 PM Peter Colpaert <peter.colpaert@xxxxxxxxx> wrote:
thanks for your (somewhat confusing but still very enlightening)
explanation.
So if I understand correctly, array elements (such as objects) cannot have
names.
That's correct, but it sounds like your understanding is still a bit tenuous.
I think the key part of Scott's explanation was this passage:
A[n RPG] data structure is equivalent to an "object" in JSON.
An [RPG] array is equivalent to an array in JSON. Each element
in an object can have a name. Each element in an array does not
have a name, but programs can refer to them as element 1,
element 2, etc.
I would strengthen that to say: Each element in a JSON object *must*
have a name.
While the "array" concept in most languages is strongly analogous to
JSON's arrays, RPG's data structure isn't that strongly analogous to a
JSON object, but it's the closest thing RPG has.
Do you know any other programming languages? Have you heard of
associative arrays? In other languages, the same concept could be
called a map, dictionary, or hash table. A JSON object is one of
these.
Basically, the essential difference between a "regular" array and an
associative array is simply how you refer to the elements. As Scott
mentioned, elements in a regular array are referred to by their
ordinal positions within the array, while elements in an associative
array are referred to by their names. (This is why they *must* have
names. Otherwise, there would be no way to refer to them.)
In principle, the elements in an associative array do not have any
particular order. These two associative arrays (or maps, or
dictionaries, or hash tables, or JSON objects) are equivalent:
o1 = {"name": "Jim", "age": 49, "height": 1.8}
o2 = {"age": 49, "height": 1.8, "name": "Jim"}
Why? Because o1["name"] has the same value as o2["name"], and
o1["height"] has the same value as o2["height"], and o1["age"] has the
same value as o2["age"]. All three elements match.
The following two JSON arrays are most definitely NOT equivalent:
a1 = ["Jim", 49, 1.8]
a2 = [49, 1.8, "Jim"]
The indices in JSON start at 0, so we would compare a1[0] with a2[0]
and find we've already hit different values ("Jim" versus 49). Indeed,
*none* of the elements match.
So, hopefully that solidifies your understanding of JSON objects versus arrays.
Now, you may have noticed that JSON arrays are quite different from
RPG arrays in an important respect: The elements may be of
heterogeneous types. I hope that didn't add to the confusion.
John Y.
As an Amazon Associate we earn from qualifying purchases.