Reading and writing globals by script

Mark Wieder ahsoftware at sonic.net
Sat Aug 10 21:21:27 EDT 2019


On 8/10/19 12:00 PM, David Epstein via use-livecode wrote:
> I want to write a function that will read and write to any named global.  What I have below seems to work, but I’m not sure quite why.  Usually, “put gName into oldVal” for a global named gName would put the value, not the name, of that global into oldVal.  But here it does not.
> 
> function globalSwap gName,gVal
>     -- Return the existing value of global gName and load gVal into that same global
>     do "global" && gName
>     do "put" && gName && "into oldVal" # "put gName into oldVal" doesn't work here.
>     do "put gVal into" && gName
>     return oldVal
> end globalSwap
> 
> Is there some way to write this with fewer or no uses of “do”?

Well, the best way, of course, is not to use globals.
Barring that (watch the wordwrap),

function globalSwap gname, pVal
    local tOldVal, tCommand
    put "global"&&gName&&";put"&&gName&&"into 
tOldVal;put"&&pVal&&"into"&&gName into tCommand
    do tCommand
    return tOldVal
end globalSwap

-- 
  Mark Wieder
  ahsoftware at gmail.com




More information about the use-livecode mailing list