Credits Overview Plotting Styles Commands Terminals

local

Syntax:

     local foo = <expression>
     local array foo[size]

The local keyword introduces declaration of a variable whose scope is limited to the execution of the code block in which it is declared. Declaration is optional, but without it all variables are global. If the name of a local variable duplicates the name of a global variable, the global variable is shadowed until exit from the local scope. See scope.

Local declarations may be used to prevent a global variable from being unintentionally overwritten by a call or load statement. They are particularly useful inside a function block. The local command is also valid inside the code block in curly brackets following an if, else, do for, or while statement.

Example: Suppose you want to write a script "plot_all_data.gp" containing commands that plot a bunch of data sets. You want to call this convenience script from the command line or from other scripts without worrying that it trashes any variables with names "file" or "files" or "dataset" or "outfile". The variable "file" is inherently local because it is an iteration variable (see scope) but the other three names need keyword local to protect them.

plot_all_data.gp:

     local files = system("ls -1 *.dat")
     do for [file in files] {
        local dataset = file[1:strstrt(file,".dat")-1]
        local outfile = dataset . ".png"
        set output outfile
        plot file with lines title dataset
     }
     unset output