Composer - local variable/table cleanup

So i am just starting to implement tables

local data = {}

type into my code and have come up with a question.

So i have a local function

[lua]

local choice = {}

local ranNum = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

local rdny

local function RandomSelection()

local s

for s = 1, 5 do

rdny = math.random(#ranNum

choice[s] = ranNum[rdny]

table.remove(ranNum, rdny) --remove that number.

end

end

– Option 2

local choice = {}

local function RandomSelection()

local ranNum = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

local rdny

local s

for s = 1, 5 do

rdny = math.random(#ranNum

choice[s] = ranNum[rdny]

table.remove(ranNum, rdny) --remove that number.

end

end

[/lua]

So the first version forward declares choice, the table of possible ranNum , and rdny .

These can obviously be used within the function.

In option 2 i only declare choice outside as it will be used elsewhere in code, but ranNum and rdny are only needed for that function.

I wish ranNum to always have the possible 15 choices  - to do this i believe it needs to be within the function so that each time the function is called the table is “repopulated” with all 15 numbers.

I assumed that if i were to do option 1 - declare the table - then do randomSelection after 3 - table would be empty and on 4th call produces an error.

By using prints i have confirmed this in the terminal.


So my feeling is option 2 is the best way to proceed. am i correct?

Question - So option 2 i declare ranNum, rdny & s for that matter as local within the function - What happens to these variables after the function is finished??

Technically ranNum will still have 10 numbers still in the table - So a Valid Variable?

Does lua say - " hey function finished - they were local to that function so hey don’t need them anymore - lets delete them from memory"

Or for correct programming do i need to do something to clean them up??

Thanks for any help clarification.

T.

You are correct.

In Option 2 ranNum and rdny are local and will fall out of scope (and be nil) outside the function.

Scoping in Lua is very granular. It’s not only function-wide.

As an example. Local variables declared within an if-statement or for-loop will only be visible inside those respective statements regardless of the function.

PS.

If you want to seriously dive into Lua, then I’d recommend getting the following book:

http://www.lua.org/pil/

It’s a great resource!

@ingemar

Thanks for the response

The key thing there was - “and be nil”  - So even if they had value within the function - Outside their value is nil. And lua will at some point go through checking for nil’s to then delete.

Now with ranNum - after function it becomes nil - but if function is called again it will repopulate the table with only 15 items , or is the correct term not “repopulate” but “create again”?

Yes i had read about the scoping ( after being taught by trial and error) and the “only visible” aspect But it’s great to confirm what i was thinking/ hoping re memory/ nil -ing / and garbage collection.

T.

Now with ranNum - after function it becomes nil - but if function is called again it will repopulate the table with only 15 items , or is the correct term not “repopulate” but “create again”?

It’s more like “create”. What happens is that these types of variables are pushed onto a stack and at the end of the function (or statement block) they’re popped off the stack and cease to exist.

Thanks,

In my previous code, my itialian “spaghetti” phase, i had a lot of holding variables , and as i progressed and read started to pre define as local - but then run into problems of only allowed 60 or under locals.

So now that i am converting from storyboard to composer and also preping for Android, and attempting to reduce my code lines via tables and do loops - things are getting complicated.

But this tidbit of knowledge will be very useful to help unclutter my code lines.

Much appreciated.

T.

You are correct.

In Option 2 ranNum and rdny are local and will fall out of scope (and be nil) outside the function.

Scoping in Lua is very granular. It’s not only function-wide.

As an example. Local variables declared within an if-statement or for-loop will only be visible inside those respective statements regardless of the function.

PS.

If you want to seriously dive into Lua, then I’d recommend getting the following book:

http://www.lua.org/pil/

It’s a great resource!

@ingemar

Thanks for the response

The key thing there was - “and be nil”  - So even if they had value within the function - Outside their value is nil. And lua will at some point go through checking for nil’s to then delete.

Now with ranNum - after function it becomes nil - but if function is called again it will repopulate the table with only 15 items , or is the correct term not “repopulate” but “create again”?

Yes i had read about the scoping ( after being taught by trial and error) and the “only visible” aspect But it’s great to confirm what i was thinking/ hoping re memory/ nil -ing / and garbage collection.

T.

Now with ranNum - after function it becomes nil - but if function is called again it will repopulate the table with only 15 items , or is the correct term not “repopulate” but “create again”?

It’s more like “create”. What happens is that these types of variables are pushed onto a stack and at the end of the function (or statement block) they’re popped off the stack and cease to exist.

Thanks,

In my previous code, my itialian “spaghetti” phase, i had a lot of holding variables , and as i progressed and read started to pre define as local - but then run into problems of only allowed 60 or under locals.

So now that i am converting from storyboard to composer and also preping for Android, and attempting to reduce my code lines via tables and do loops - things are getting complicated.

But this tidbit of knowledge will be very useful to help unclutter my code lines.

Much appreciated.

T.