Nested stack properties...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Nested stack properties...

Post by stam » Fri Jul 31, 2020 6:35 pm

Hi all,
I was hoping to replicate a moderately complex data structure using a custom property in the stack.

I was hoping to create a custom property set (call it 'root') with multiple child nodes ('node_x'), of all of which will have about 18 sub-nodes('subnode_x').

I can create this easily enough in the IDE - but can't figure out the correct way to access this in script.

As a test, i manually put some random text in subnode_x. Using the Message box:

Code: Select all

put the root["node_x"]["subnode_x"] of this stack
generates an error: Script compile error: Error description: Expression: bad factor

I'm sure i'm doing it 'wrong'. All the examples i found online refer to 1-deep nodes only (ie. the structure would be root[node_x] without further subnodes. Is that the problem here? Certainly if i use root[node_x] only with no further subnodes, this works as intended...

Thanks in advance for putting me straight!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Nested stack properties...

Post by dunbarx » Fri Jul 31, 2020 7:59 pm

Hi.

Syntax issues, likely. Try this in a button;

Code: Select all

on mouseup
   put 6 into root["a"]
   set the testProp of this cd to root
   get the testProp of this cd
   combine it with comma --and comma
   answer it
end mouseup
Craig

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

Re: Nested stack properties...

Post by stam » Fri Jul 31, 2020 8:55 pm

Thanks Craig,

I thought i'd done something like this and it worked - but it wasn't what i was looking for.
Unless I'm completely missing the point of your code? (very possible!).

I had tried the following which works:

Code: Select all

set the root[node_x] of this stack to "xxx"
put the root[node_x] of this stack --> outputs 'xxx' in the message box
I think this is equivalent to your example (again forgive me if i'm wrong!), but what I'm was trying to achieve was a bit different.

Basically root is an array of nodes and nodes are arrays of subnodes and i'm trying to get/set the subnode, but failing...
The structure of the custom property is:

Custom Property Set 'Root'
--------<node_1>
-------------------<subnode_1>
-------------------<subnode_2>
...
-------------------<subnode_18>
--------<node_2>
-------------------<subnode_1>
-------------------<subnode_2>
...
-------------------<subnode_18>
...
--------<node_18>
-------------------<subnode_1>
-------------------<subnode_2>
...
-------------------<subnode_18>

I imagined if i wanted to get/set the subnode_4 of node_18 of root, the syntax would be

Code: Select all

set the root[node_18][subnode_4] of this stack to "xxx" --> this does not generate an error but nothing seems to happen
put the root[node_18][subnode_4] of this stack  --> Script compile error: Error description: Expression: bad factor
If i keep this 1 level deep (ie not have any subnodes, just have root as an array of nodes) this works fine.
But not if > 1 level deep it seems... wondering if i'm still doing it wrong or if that's a characteristic of custom properties?

Very happy to be corrected/put straight on this :)

SparkOut
Posts: 2862
Joined: Sun Sep 23, 2007 4:58 pm

Re: Nested stack properties...

Post by SparkOut » Fri Jul 31, 2020 11:19 pm

As far as I know, you can't directly set a custom property more than one level deep. You can set an element of a custom property to an array so as to have subnodes, but I think you will not be able to address an individual subnode element of a custom property without copying the data out into a variable, and addressing it there, then writing the variable back into the custom property.
You might be able to do something with a bit of arcane / judicious use of custom property sets which may pseudo-work slightly more directly on a custom property, such as

Code: Select all

set the customProperties["root"] of me to theArray
which will then elevate the "root" array to an independent customKey. But you still won't be able to use multi-level array notation to access either, without first getting the values out into a variable and then plonking the variable back into the custom property. I think.

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

Re: Nested stack properties...

Post by stam » Fri Jul 31, 2020 11:25 pm

SparkOut wrote:
Fri Jul 31, 2020 11:19 pm
... I think you will not be able to address an individual subnode element of a custom property ...
Thanks SparkOut - I suspected as much.
Was just asking as you can easily create these structures in the IDE but it seems bizarre that these are inaccessible from code :-(

Would have been nice to have direct access to the data in the IDE...Think maybe LC may be missing a trick here, but i'm new here, what do i know lol :)

I'll store an array directly - thank you.

SparkOut
Posts: 2862
Joined: Sun Sep 23, 2007 4:58 pm

Re: Nested stack properties...

Post by SparkOut » Fri Jul 31, 2020 11:41 pm

Yes, it is a bit odd but it's easy enough to copy to a variable for work and then store again. You get used to it. Similarly you can't do something like

Code: Select all

set the cCounter of me to the cCounter of me + 1
you have to copy the value out, add 1, then write the new value back. Also to delete a key you have to copy the customKeys, delete the unwanted line, and reset the customKeys.

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

Re: Nested stack properties...

Post by stam » Sat Aug 01, 2020 12:59 am

Yeh i think i've encountered the first issue where to change a property you have to put it into a variable and write it back... will keep these idiosyncrasies in mind... thank you

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Nested stack properties...

Post by FourthWorld » Sat Aug 01, 2020 5:32 am

The section in the User Guide (in the IDE, see the second item in the Help menu) on custom properties provides a good orientation. It includes a discussion of "Using multiple custom property sets" on p 154 that will get you using them well.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Nested stack properties...

Post by stam » Sat Aug 01, 2020 1:51 pm

I've been largely Googling for info as and when needed. You are entirely correct Richard - the User Guide PDF is very helpful, thanks for pointing this out -- will definitely make it my first port of call...
I'm still surprised at how 'expressive' the liveCode language is...

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Nested stack properties...

Post by AxWald » Sat Aug 01, 2020 2:23 pm

Richard,

Thx, this was a helpful hint! Didn't know about this PDF, after all these years ...

I'll try to explain the customProperty/ array confusion:
(this is as I understand it, anybody feel free to correct me!)
User Guide wrote:All the custom properties in a custom property set form an array. The array's name is the
custom property set name, and the elements of the array are the individual custom
properties in that custom property set.
For me this translates to:

If you're using array notation addressing a customProperty,
LC only thinks of the customProperties and customPropertySets,
and NOT of the contents of an array stored in any of these!


So:

Code: Select all

the cProp["myProp"] of this stack
refers to the customProperty "myProp" of the customPropertySet "cProp" of the current stack!

And:

Code: Select all

   put "one" into myArray[1]
   put "two" into myArray[2]
   set the cProp of this stack to myArray
   answer the cProp[1] of this stack
returns empty - there is no customPropertySet "cProp" with a customProperty "1" :/

But there's a way:
User Guide wrote:If you store a set of custom properties in a custom property set, the set can be used just
like an array. You can think of the custom property set as though it were a single custom
property, and the properties in the set as the individual elements of the array.
So we have to:

Code: Select all

   put "one" into myArray[1]
   put "two" into myArray[2]
   set the customProperties["myProp"] of this stack to myArray
   answer the myProp[1] of this stack
We created a customPropertySet "myProp" with 2 customProperties ("1" & "2"), and the answer is "one". Bingo!

Unfortunately, we'll hit the wall if we try to use nested arrays:

Code: Select all

   put "one" into myArray[1]["vEng"]
   put "uno" into myArray[1]["vIta"]
   put "two" into myArray[2]["vEng"]
   put "due" into myArray[2]["vIta"]
   set the customProperties["myProp"] of this stack to myArray
   answer the myProp[1]["vIta"] of this stack  --  <= This doesn't work!
The last line doesn't compute :/

Additionally, we cannot retrieve the whole former myArray once we stored it:

Code: Select all

   put the myProp of this stack into myNewArray
This results in an empty variable :/ (after all, it's a whole customPropertySet!)

We only can grab the entries one at a time:

Code: Select all

   put the myProp[1] of this stack into myNewArray
   answer myNewArray["vIta"]
Conclusion:
If we need to work with arrays that are more than simple key/value pairs, then the best choice is just to throw the whole thing into a simple customProperty:

Code: Select all

   put "one" into myArray[1]["vEng"]
   put "uno" into myArray[1]["vIta"]
   put "two" into myArray[2]["vEng"]
   put "due" into myArray[2]["vIta"]
   ...
   set the cMyPhatArray of this stack to myArray
From there, we can easily read, sort & modify it, and write it back afterwards:

Code: Select all

   put the cMyPhatArray of this stack into myArray
   answer myArray[2]["vIta"]
   put "three" into myArray[3]["vEng"]
   put "tre" into myArray[3]["vIta"]
   set the cMyPhatArray of this stack to myArray
So copy the cProp to a variable, play with it, write it back.

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

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

Re: Nested stack properties...

Post by stam » Sat Aug 01, 2020 2:35 pm

Thanks AxWald - very helpful.

Was mainly asking as the IDE allows you to endlessly add subproperties to properties:

Image

For my particular project would have been helpful to view the data directly in the IDE as it's a fairly complex data structure - but no such luck...

Arguably, the IDE should not allow you to create these, since in no way can you use them (if i've understood the issues correctly) - that's what the confusion was about :wink:

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

Re: Nested stack properties...

Post by Klaus » Sat Aug 01, 2020 2:46 pm

Hi Stam,
stam wrote:
Sat Aug 01, 2020 2:35 pm
...
For my particular project would have been helpful to view the data directly in the IDE as it's a fairly complex data structure - but no such luck...
don't give up so early! 8)

The "Treeview" widget can do exactly that, display array data in the IDE (and runtime of course)!
Create a new treeview widget and:

Code: Select all

...
set the arraydata of widget "your treeview widget here..." to whatever_array_you_want_to_display
## To use one of your examples:
## set the arraydata of widget "your treeview widget here..." to the root of this stack
...
Best

Klaus

P.S.
The "custom properties" part of the LC Inspector is using this widget!

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

Re: Nested stack properties...

Post by Klaus » Sat Aug 01, 2020 3:05 pm

If you want to learn more about the "expressive"-ness of LC, maybe you want to take a quick look at these stacks:
http://www.hyperactivesw.com/revscriptc ... ences.html

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

Re: Nested stack properties...

Post by stam » Sat Aug 01, 2020 6:17 pm

Klaus wrote:
Sat Aug 01, 2020 2:46 pm
The "Treeview" widget can do exactly that, display array data in the IDE (and runtime of course)!
Thanks Klaus - will keep Treeview in mind (although it's not what i'm looking for this project - the viewing of data was mainly for testing purposes).

I presume then that the same still applies - i create a multidimensional array in code and drop it into a treeview control?
If that's the case, still have to ask what is the purpose of the functionality in the IDE shown in the screenshot above - i really don't understand why it's there and hope i'm not missing something.

Thanks also for the HyperactiveSW link - i had already spotted this one on one of my google searches... the content is good even if the web design hasn't kept up with the times ;)

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

Re: Nested stack properties...

Post by Klaus » Sat Aug 01, 2020 6:27 pm

Hi Stam,
stam wrote:
Sat Aug 01, 2020 6:17 pm
...
I presume then that the same still applies - i create a multidimensional array in code and drop it into a treeview control?
yes, as long as you "drop" it by script. :D
stam wrote:
Sat Aug 01, 2020 6:17 pm
...
If that's the case, still have to ask what is the purpose of the functionality in the IDE shown in the screenshot above - i really don't understand why it's there and hope i'm not missing something.
Sorry, cannot answer this almost philosophical question, but I find it quite helpful to be able to modify an array stored in a custom property, although you cannot access the keys directly.


Best

Klaus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”