[Home] [Downloads] [Search] [Help/forum]

local

Local variables are created with the local statement. Their scope is limited to the block where they are declared.

eg.


a = 1 -- global variable
local b = 2 -- local variable


Global variables are stored in the "global environment table" which is generally accessible to all functions. Local variables are only visible to the block in which they are "seen". For example:


function foo ()
  local x = 42  -- x is only visible inside foo
  y = 63  -- global variable y is changed
end -- function foo


In this example if there was another variable "x" somewhere else in the program, it would not be affected by the local variable x inside foo.

Be aware that if you redefine a local variable you get a new one of the same name. Eg.


function foo ()
  local x = 42  -- x is only visible inside foo
  
  -- do various things here

  local x = 43  -- this is a new variable x, we have not changed the earlier one
end -- function foo


If you want to change an existing local variable, do not use the word local again, eg.


function foo ()
  local x = 42  -- x is only visible inside foo
  
  -- do various things here

  x = 43  -- we have changed the earlier one
end -- function foo


See Also ...

Lua keywords/topics

assignment
break
comments
data types
do
for (generic)
for (numeric)
function
identifiers
if / then / else
keywords
logical operators
precedence
relational operators
repeat
return
string literals
tables
while

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: lua=local)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]