Array referencing

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Array referencing

Post by stam » Fri Oct 27, 2023 2:42 pm

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?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Array referencing

Post by Klaus » Fri Oct 27, 2023 3:40 pm

Sometimes this kind of stuff works, if you DO it. :D
At least worth a try...

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Array referencing

Post by stam » Fri Oct 27, 2023 3:48 pm

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.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Array referencing

Post by LCMark » Fri Oct 27, 2023 4:31 pm

@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

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Array referencing

Post by stam » Sat Oct 28, 2023 7:05 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Array referencing

Post by mwieder » Sun Oct 29, 2023 11:29 pm

@LCMark-

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Array referencing

Post by LCMark » 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).

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Array referencing

Post by rkriesel » Fri Nov 03, 2023 7:49 pm

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

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”