Custom Properties and nested arrays

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

Post Reply
j9v6
Posts: 80
Joined: Tue Aug 06, 2019 9:27 am
Location: U.K.
Contact:

Custom Properties and nested arrays

Post by j9v6 » Wed May 08, 2024 10:28 am

Hi all,

I've read a whole lot of forum posts about custom properties over the last couple of weeks so I'm fine with things such as

set the cCurrentWeather of this stack to "Sunny"

and

put the cCurrentWeather of this stack etc

What I'm now trying to do is find a way to reference nested arrays. I can manually create the nesting levels just fine, but I'm struggling to find a way to interact with them programatically both to set values and retrieve values.

Here's a sample set of manually created custom properties
customPropTest.jpg
Custom Properties - stack level
So the nested custom property array is called animals and has sub-types of cat and dog. Each sub type contains the pet name and their preferred food. What I'd like to be able to do is add a new animal to the nested array along with their preferred food, and also do a queries on the custom properties to retrieve all the cats names or the dogs names, or see what food animals>dog>scoooby likes to eat for example. Now, this is just a test stack to see how it all works and yes I know I could do this with a sqlite database too.

While playing around with the above I've found an alternate method which is kind of ok. I created two other customPropertySets: one for cats and one for dogs then stored the pet name and preferred food within those. I don't mind using customProperties this way, but it bugs me that I can create a nested array in the standard customProperties but I can't find a way to work with them through LC.

Is there a magic way to delve down into the nested levels of these custom properties??????

Thanks for any pointers and help

Al.

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

Re: Custom Properties and nested arrays

Post by Klaus » Wed May 08, 2024 10:45 am

Hi Al,

using your array for my example:

Code: Select all

...
## Get a value:
answer the animals["dogs"]["rufus"] of this stack
## -> chum
...
## Set a value
set the animals["dogs"]["rufus"] of this stack to "fido"
...
## Create a new key and value:
set the animals["dogs"]["pluto"] of this stack to "your name for pluto here..."
...
See, the only "trick" is to use square brackets.

Hint:
I always use a c as a "prefix" for custom properties, so I can recognize them from afar :-) -> the cAnimals of this stack

Best

Klaus

j9v6
Posts: 80
Joined: Tue Aug 06, 2019 9:27 am
Location: U.K.
Contact:

Re: Custom Properties and nested arrays

Post by j9v6 » Wed May 08, 2024 11:06 am

Hi Klaus,

Thanks for the response. If I try answer the animals["dogs"]["rufus"] of this stack from the Message Box I get
Script compile error:
Error description: Expression: bad factor
I've tried adding a button to the card too the same code
customPropertyTest1.jpg
but as you can see it throws an error as well. Any ideas?

???

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

Re: Custom Properties and nested arrays

Post by Klaus » Wed May 08, 2024 12:00 pm

Sorry, in a hurry right now, will check this later today...

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

Re: Custom Properties and nested arrays

Post by stam » Wed May 08, 2024 1:18 pm

Generally I'm not sure you can use the custom props as an array as is and have found I have to put them into their own array.
This should work:

Code: Select all

Put the animals of this stack into tArray
answer tArray["dogs"]["rufus"]
S.

j9v6
Posts: 80
Joined: Tue Aug 06, 2019 9:27 am
Location: U.K.
Contact:

Re: Custom Properties and nested arrays

Post by j9v6 » Wed May 08, 2024 1:40 pm

Thanks for this. It works!

Plus LC have just authored a lesson to address this exact quey

https://lessons.livecode.com/m/4071/l/1 ... ted-arrays

😊👍

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

Re: Custom Properties and nested arrays

Post by Klaus » Wed May 08, 2024 2:56 pm

Yes, that was it. :-)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”