How can we dynamically create variable names from changing value "x" on a loop?

Mike Bonner bonnmike at gmail.com
Sun Nov 6 12:13:02 EST 2016


Why not just leave it as an array?  In fact, if you want to have separate
preferences categories, that's easy enough to do too.

local sPrefsA

put getPref ("preferences/modules/color-meditation") into
sPrefsA[ColorMedPrefs]

at this point you have an local script array variable structured like so..
sprefsA
      colormedprefs
                  breathcount = 5
                  breathPace = 1
                  cycles = 2
                  audio on = true

to get a value from above, a simple function works..

function getSinglePref prefCat,prefName
     if prefcat is among the keys of sPrefsA
         if prefName is among the keys of sPrefsA[prefCat] then
              return sPrefsA[prefCat][prefName]
         else
              answer information "No such preference"
         end if
     else
          answer information "No such preference category"
      end if
end getSinglePref

If you don't need to categorize, change it to this..

put getPref ("preferences/modules/color-meditation") into sPrefsA

at which point you can retrieve a value with sPrefsA[prefname]


The code above is just off the top of my head, but it should be close.

On Sun, Nov 6, 2016 at 8:50 AM, Sannyasin Brahmanathaswami <brahma at hindu.org
> wrote:

> Given this scenario:
>
> We fetch a preference array from some json on disk
> We want to insert the key-values into separate discrete local vars in the
> stack script
>
> function getUserPreferences
>
> # the following function fetches an object in a JSON file:
>
> put getPref ("preferences/modules/color-meditation") into aColorMedPrefs
>
> # aColorMedPrefs now appears in variable watcher with keys
> # aColorMedPrefs["BreathCount"] # value = 5
> # aColorMedPrefs["BreathPace,"] # value = 1
> # aColorMedPrefs["Cycles "] # value = 2
> # aColorMedPrefs["AudioOn"] # value = "true"
>
> # we want to pass each to a discrete local:
> # sBreathCount,sBreathPace,sCycles,sAudioOn
>
>          repeat for each key x in aColorMedPrefs
> put "s" & x into tNextPref
> put aColorMedPrefs[x] into tNextPref
> put tNextPref & comma after tSettings
> end repeat
>
> return tSettings
>
> # result:  "5,1,2,true"  i.e. the values
>
> # But what we really want to do was insert those values in the local vars
> on each iteration.
>
>   return (sBreathCount,sBreathPace,sCycles,sAudioOn) # would also return
> "5,1,2,true"    # but we get ",,," i.e now values.
> end getUserPreferences
> ------------
> i.e. how do we dynamically create/name/instantiate variables & set their
> values from values in a loop?
>
> it begs for some syntax like
>
> create var ("s" & x); put x into the last var
>
> BR
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list