Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "LUA"

From NetSec
Jump to: navigation, search
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
LUA is a portable [[interpreted languages|interpreted language]] used both by [[nmap|NMAP]] and ironically the MMORPG World of Warcraft.
+
Lua is a portable [[interpreted languages|interpreted language]]. It is mainly used in Games, however it is also used by [[nmap|NMAP]]'s Scripting Engine
[[Category:Programming Languages]]
+
 
 +
<font size="-2">Special thanks to [[User:Trep|Trep]] for his contributions to this article.</font>
 +
=Comments=
 +
{{code|text=
 +
<source lang="lua">
 +
-- This is a single line comment
 +
</source>
 +
}}
 +
{{code|text=
 +
<source lang="lua">
 +
--[[
 +
  This
 +
  is
 +
  a
 +
  multi-line
 +
  comment
 +
  ]]
 +
</source>
 +
}}
 +
 
 +
 
 +
=Variables=
 +
Variables in Lua are very simple. it is not required to declare the variable's data type, just assign a value.
 +
{{code|text=
 +
<source lang="lua">
 +
Var = 1
 +
Var2 = "Im a var!"
 +
Var3 = 2
 +
Var4 = Var * Var3
 +
 
 +
</source>
 +
}}
 +
==Global Variables vs Local Variables==
 +
 +
When Variables in lua are declared, they are globally accessible unless otherwise specified with ''local''.
 +
 
 +
{{code|text=
 +
<source lang="lua">
 +
GlobalVar = 0
 +
local LocalVar = 1337
 +
</source>
 +
}}
 +
 
 +
=Functions=
 +
Functions in lua are quite easy as well.
 +
{{code|text=
 +
<source lang="lua">
 +
function SimpleAddition(x,y,z)
 +
  local Sum = x + y + z --only accessible from within this context.
 +
  SumTimesTwo = Sum * 2 -- can be referenced from anywhere in the current file, or wherever file is loaded.
 +
  return Sum
 +
end
 +
</source>
 +
}}
 +
 
 +
 
 +
=Tables=
 +
Lua tables are a bit like arrays, except that an array is.. well an array of values.
 +
A Table, is an array of Keys, and those Keys have values.
 +
 
 +
 
 +
====Declaring an empty Table====
 +
{{code|text=
 +
<source lang="lua">
 +
MyTable = {} --declares an empty table.
 +
</source>
 +
}}
 +
====Declaring, and populating a Table====
 +
{{code|text=
 +
<source lang="lua">
 +
Numbers = {"one","Two","Three","Four","Five"}
 +
</source>
 +
}}
 +
The above declaration is exact to the one below
 +
{{code|text=
 +
<source lang="lua">
 +
Numbers = {}
 +
Numbers[1] = "one"
 +
Numbers[1] = "Two"
 +
Numbers[1] = "Three"
 +
Numbers[1] = "Four"
 +
Numbers[1] = "Five"
 +
</source>
 +
}}
 +
 
 +
====Indexing Tables====
 +
{{Protip|Unlike what you would expect, the index for a table starts at 1, instead of 0.}}
 +
{{code|text=
 +
<source lang="lua">
 +
Numbers = {"one","Two","Three","Four","Five"}
 +
print(Numbers[0]) -- prints nil, as index starts at 1.
 +
print(Numbers[1]) -- prints "one"
 +
</source>
 +
}}
 +
 
  
 
{{programming}}{{social}}
 
{{programming}}{{social}}
 +
 +
[[Category:Interpreted languages]][[Category:Programming Languages]]

Latest revision as of 03:43, 20 September 2012

Lua is a portable interpreted language. It is mainly used in Games, however it is also used by NMAP's Scripting Engine

Special thanks to Trep for his contributions to this article.

Comments

 
-- This is a single line comment
 
 
--[[
  This
  is
  a
  multi-line
  comment
  ]]
 


Variables

Variables in Lua are very simple. it is not required to declare the variable's data type, just assign a value.

 
Var = 1
Var2 = "Im a var!"
Var3 = 2
Var4 = Var * Var3
 
 

Global Variables vs Local Variables

When Variables in lua are declared, they are globally accessible unless otherwise specified with local.

 
GlobalVar = 0
local LocalVar = 1337
 

Functions

Functions in lua are quite easy as well.

 
function SimpleAddition(x,y,z)
   local Sum = x + y + z --only accessible from within this context.
   SumTimesTwo = Sum * 2 -- can be referenced from anywhere in the current file, or wherever file is loaded.
   return Sum
end
 


Tables

Lua tables are a bit like arrays, except that an array is.. well an array of values. A Table, is an array of Keys, and those Keys have values.


Declaring an empty Table

 
MyTable = {} --declares an empty table.
 

Declaring, and populating a Table

 
Numbers = {"one","Two","Three","Four","Five"} 
 

The above declaration is exact to the one below

 
Numbers = {} 
Numbers[1] = "one"
Numbers[1] = "Two"
Numbers[1] = "Three"
Numbers[1] = "Four"
Numbers[1] = "Five"
 

Indexing Tables

Protip: Unlike what you would expect, the index for a table starts at 1, instead of 0.
 
Numbers = {"one","Two","Three","Four","Five"} 
print(Numbers[0]) -- prints nil, as index starts at 1.
print(Numbers[1]) -- prints "one"
 


LUA is part of a series on programming.
<center>
</center>