lua-users home
lua-l archive

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


In the 4.0.1 reference, in the section on table for loops (4.4.4), it
says "- The behavior is undefined if you change the table _t during
the traversal."  But I'm not sure what "change" means.

If I have the following code:
    t = { x = "one", y = "seven", foo = "four" };
    for i, val in t do
        t[i] = strupper(val);
        print(i, t[i]);
    end
is it undefined or not?  In my tests, it seems to work, but just
because it works doesn't mean that it's not undefined.  <grin>  Do I
need to create another table to copy the changed values into, to avoid
undefined behavior?

I'm not sure if changing the value associated with an array index
constitutes changing the array, or whether that only means things like
adding or deleting array indexes.

Ed

BTW, I'm evaluating Lua as a way to replace a bunch of ugly shell
scripts in a Linux installer intended for installing on older
machines.  Lua's small footprint on disk and memory, as well as its
expressiveness, are what attracted me to the language.