@trevix: If I understand what you want to do correctly, then you want to be able to access a deeply nested array element differently, based on some condition. To handle such cases, you can pass a 'sequence' as the key of an array which the engine will then use to dynamically descend into the array.
So, let's say you have an array (using LC10, array literal notation):
Code: Select all
put { "a": { "b": [10, 20, 30] }, \
"b": { "a": [10, 20, 30] } } into tArray
Here there are two top-level keys, with slightly different structure underneath.
Let's say you want to access the second element of the nested sequence (i.e. value '20'). With normal array access you'd do:
Code: Select all
put tArray["a"]["b"][2] into tAValue
put tArray["b"]["a"][2] into tBValue
However, using the 'dynamic' feature you can also do it like this:
Code: Select all
put [ "a", "b", 2 ] into tAPath
put tArray[tAPath] into tAValue
put [ "b", "a", 2 ] into tBPath
put tArray[tBPath] into tBValue
So what you are trying to do in your (latest) code snippet is 'close' to what you can do, but the real version would be something more like:
Code: Select all
function PathToPref pValue
if 1 = 1 then --this is where the choice is made
put [ "gfPrefTvBox", gSport, "setting", pValue ] into tPathToPref
else
put [ gSport, "setting", pValue ] into tPathToPref
end if
return tPathToPref
end PathToPref
This function returns the 'path' to the key you want to manipulate. Then to set the 'DEFAdvantage' sub-key you'd do:
Code: Select all
put "regular" into gPrefTF[PathToPref("DEFAdvantage")]
The 'dynamic' path stuff is pretty flexible as any subscript expression *can* be an array, in which case the engine concatenates the resulting sequences before evaluating the element (or setting it). An alternative to the above would be (cut down version):
Code: Select all
put [ gSport, "setting" ] into tPathToPref
put "regular" into gPrefTF[tPathToPref]["DEFAdvantage"]
One thing you can't do (which you tried to do in your original code) was to select a different (root) variable dynamically - that's only possible by using do - but if you have a single root variable, with the first key selecting which 'root path' you want to take (like you have done) - then you should be good with the dynamic path stuff outlined above.