Custom Properties and nested arrays
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Custom Properties and nested arrays
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
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.
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
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.
Re: Custom Properties and nested arrays
Hi Al,
using your array for my example:
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
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..."
...
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
Re: Custom Properties and nested arrays
Hi Klaus,
Thanks for the response. If I try answer the animals["dogs"]["rufus"] of this stack from the Message Box I get
but as you can see it throws an error as well. Any ideas?
???
Thanks for the response. If I try answer the animals["dogs"]["rufus"] of this stack from the Message Box I get
I've tried adding a button to the card too the same codeScript compile error:
Error description: Expression: bad factor
but as you can see it throws an error as well. Any ideas?
???
Re: Custom Properties and nested arrays
Sorry, in a hurry right now, will check this later today...
Re: Custom Properties and nested arrays
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:
S.
This should work:
Code: Select all
Put the animals of this stack into tArray
answer tArray["dogs"]["rufus"]
Re: Custom Properties and nested arrays
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
Plus LC have just authored a lesson to address this exact quey
https://lessons.livecode.com/m/4071/l/1 ... ted-arrays
Re: Custom Properties and nested arrays
Yes, that was it.