[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setting global variables

From: Ian McCallion
Subject: Re: setting global variables
Date: Sat, 27 Oct 2018 06:25:07 +0100

On Fri, 26 Oct 2018, 18:41 Fritz Sonnichsen, <address@hidden> wrote:
I have been using the "global" directive to set up some (around 50)
global variables in a script. However in order to access them from
functions in the same script, I need to define them as global again or
they are not defined in the function.
    Seems a bit redundant to me--is there some way to define variables
share by all functions just once?

The language is defined that way for good practical reasons so there is no way to do exactly what you want. My way round the problem is to define a single global structure:

  global ggg

All my functions that need any global data include this statement. Then all my global variables are fields of this structure:

   ggg.hopsize = 10;

Doing this is actually quite conveniently self-documenting and it is useful during debugging to be able to display all the global values simply by typing:


at the console.

The other idea I tried and eventually rejected, which only works if you are using globals as a way of sharing constants, is to define a trivial function in its own file to embody the value:

    function [ret] = windowsize()
        ret = 50;

In any function you can then use windowsize in an _expression_ just as though it were a variable, but obviously using it on the left of an assignment statement will not have the desired effect.

Hope this helps.

Cheers... Ian

reply via email to

[Prev in Thread] Current Thread [Next in Thread]