HOW-TO: locals.
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HOW-TO: locals.

Started by plop, 19 June, 2005, 18:05:06

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

plop

locals on LUA 5 are more local then on LUA 4.
here I'll try 2 explain how local they really are.
and how 2 use them in the best possible way.
like always you can execute the example scripts on the
LUA (5) command line, which can be found on my website.

--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

-- lets loop the table and stuff them in another local table.
for a,b in ipairs(number1) do
	-- check if number2 excist if not make it.
	if not number2 then
		local number2 = {}
	end
	number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
	if thetable2beprinted then
		for a,b in ipairs(thetable2beprinted) do
			print(b)
		end
	else
		print("error table missing")
	end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
that didn't work. number2 was made but wasn't there.
why ?
if not number2 then
	-- only between here and the next end number2 is around.
	-- after that lua calls it garbage.
		local number2 = {}
	end
	-- here it's gone.
lets declare it a bit sooner.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

for a,b in ipairs(number1) do
	-- lets make a empty global
	local number2
	if type(number2) ~= "table" then
		number2 = {}
	end
	number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
	if thetable2beprinted then
		for a,b in ipairs(thetable2beprinted) do
			print(b)
		end
	else
		print("error table missing")
	end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
now the table is still missing.
so we need to declare it even sooner.
lets do it on the start.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

local number2
for a,b in ipairs(number1) do
	if type(number2) ~= "table" then
		number2 = {}
	end
	number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
	if thetable2beprinted then
		for a,b in ipairs(thetable2beprinted) do
			print(b)
		end
	else
		print("error table missing")
	end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
now this works but number2 is still available.
copy the last line a couple times.
not a real local like we wanted.
it's as local as number1 is.
lets make it a function which returns the table.
with this we can send a local copy straight 2 the print function.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

function copytable(tablename)
	local number2 = {} -- making the number2 table
	for a,b in ipairs(tablename) do
		number2[a]=b
	end
	return number2
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
	if thetable2beprinted then
		for a,b in ipairs(thetable2beprinted) do
			print(b)
		end
	else
		print("error table missing")
	end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(copytable(number1))
now this is a nice way.
we have a local copy which is dropped right on the spot it wasn't needed anymore.
we can prove it by trying 2 print number2
add the next line at the end of the previous example script.
PrintOutcome(number2)
it's gone 100%.
hope you understand a bit how local locals really are.

have fun scripting.
plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Dessamator

hmm, i used some of those for variables not tables, but its a nice way to do it
Ignorance is Bliss.

SMF spam blocked by CleanTalk