lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 21-07-2010 12:23, Warlich, Christof wrote:

Hi all,

 

I haven’t really followed the extensive discussion regarding locals versus globals, so please take my apology

or hit me if I bring up something that was discussed already, but I need to ask this question because Lua’s

behaviour seems to be quite unexpected to me here: Consider the following code:

 

data = ""

for word in data:gmatch("(%S+)%s*") do

    print(word)

end

print(word) – Prints nil, so “word” must be local to the for-loop?!

 

function f()

    x = 10;

end

f()

print(x) – Prints 10 as I would expect

 

Why is “word” not a global?

 

From the manual http://www.lua.org/manual/5.1/manual.html#2.4.5

"The loop variables var_i are local to the loop; you cannot use their values after the for ends. If you need these values, then assign them to other variables before breaking or exiting the loop."

/Erik