Page 2 of 2

Re: Array referencing

Posted: Fri Oct 27, 2023 2:42 pm
by stam
Now I'm confused lol :)
LCMark wrote:
Fri Oct 27, 2023 1:43 pm
@trevix: Heh - my post was a little dense!

Perhaps a much shorter way to explain the 'dynamic path' feature is this:

Code: Select all

get tArray[A][B][C][D][E]
put tValue into tArray[A][B][C][D][E]
Is equivalent to:

Code: Select all

put [ A, B, C, D, E ] into tPath
get tArray[tPath]
put tValue into tArray[tPath]
I understand the first example of course, or at least I think it do.
It should create an array

Code: Select all

tPath[A] = empty
tPath[A][B] = empty
tPath[A][B][C] = empty
tPath[A][B][C][D] = empty
tPath[A][B][C][D][E] = tValue
What find confusing is the new syntax... does this create a nested array or are these just keys of the array - from my limited understanding, this would produce

Code: Select all

tPath[1] = A
tPath[2] = B
tPath[3] = C
tPath[4] = D
tPath[5] = E
Admittedly for preferences anything would be fine, but if I've understood the new syntax correctly you define the value top level keys (unless you pass an array I suppose) - is that right?

Re: Array referencing

Posted: Fri Oct 27, 2023 3:40 pm
by Klaus
Sometimes this kind of stuff works, if you DO it. :D
At least worth a try...

Re: Array referencing

Posted: Fri Oct 27, 2023 3:48 pm
by stam
I will - but I’m on call for 7 consecutive days, and the chance of getting close to my Mac, let alone play with code is limited to weekend after next...
At least I have my phone to keep me occupied between cases ;)
But I can only go on what I’ve seen posted.

Are you saying that

Code: Select all

put [ A, B, C, D, E ] into tPath
get tArray[tPath]
put tValue into tArray[tPath]
Creates an array where only

Code: Select all

tArray[A][B][C][D][E] = tValue
and all other keys like tArray [A][ B] are empty?

Not being argumentative, genuinely trying to understand the new syntax.

Re: Array referencing

Posted: Fri Oct 27, 2023 4:31 pm
by LCMark
@stam: So:

Code: Select all

put [ A, B, C, D, E ] into tPath
get tArray[tPath]
put tValue into tArray[tPath]
Does indeed create an array where:

Code: Select all

tArray[A][B][C][D][E] = tValue
However, the other keys are not empty - they contain the nested arrays. If you assume that A-E (which I was just using as placeholders for arbitrary expressions) are the strings A-E, then the resulting array (in JSON) is:

Code: Select all

{ "A": { "B": { "C": { "D": { "E": <value in tValue> } } } } }
Remember that the value of an array key is only a single value - its either a scalar (string etc.), or another array - they can't hold both an array and a scalar (for want of a better way to put it).

The array subscript operator is nested - ones further to the right refer to keys 'deeper' in the array hierarchy. The 'dynamic' feature I mentioned on this thread is just a dynamic way to do what you can do statically in script.

The [ A, B, C, ] (and { A: B, ... }) syntax is just the new shorthand for array literals which is more compact than using lots of puts to build them.

P.S. If my posts / response is still not clear / hard to understand - if you try and explain what you don't quite get, then I'll try again :D

Re: Array referencing

Posted: Sat Oct 28, 2023 7:05 am
by stam
Thank you for talking the time to explain Mark!

I was thinking of this like comma-separated elements rather than nested arrays… still seems a bit strange but I think I get it, thank you.

Stam

Re: Array referencing

Posted: Sun Oct 29, 2023 11:29 pm
by mwieder
@LCMark-

!!! That's brilliant !!!
I take it both new array index syntax forms are LC10+ only.

Re: Array referencing

Posted: Mon Oct 30, 2023 6:16 am
by LCMark
@mwieder: Array literalts [] and {} syntax is new in LC10, dynamic paths have been around a while (I'm tempted to say since before 6.0 but I'm not 100% sure).

Re: Array referencing

Posted: Fri Nov 03, 2023 7:49 pm
by rkriesel
LCMark wrote:
Mon Oct 30, 2023 6:16 am
@mwieder: Array literalts [] and {} syntax is new in LC10, dynamic paths have been around a while (I'm tempted to say since before 6.0 but I'm not 100% sure).
@LCMark: Comments for bug id 7166 indicate dynamic paths appeared first in release 3.5.

Dynamic paths are a great feature, but there's still no mention of them in the user guide or dictionary (or have I just missed it?).

-- Dick