Lua facts

LJ Sonik

Developer
Sonic Team Junior
This is a list of potentially interesting things you might not know about Lua.
Most of the facts are focused on the language itself and its standard libraries, rather than SRB2-specific functionality.

I will try to update it over time as I get more ideas.


  1. Lua is Portuguese and Galician for "Moon"
    The most important fact, of course.
  2. Lua is not an acronym
    Do you spell Earth's satellite MOON, M.O.O.N. or Moon?
  3. Lua tables are split into two parts: an "array part" and a "hash part"
    The array part is made of all the elements which keys are integer numbers in the range [1; n], where n is the first key that precedes a nil element (or, if there are no "holes", the last element).

    The hash part is made of (literally) all elements that do not fit into the array part.
    This does include keys that are negative numbers, keys that are zero (yes, zero is NOT a valid array index), and of course any key that is not a number, be it a string, boolean, function, userdata, etc.

    Code:
    -- The tables below only have an array part
    { 1, 2, 3, 4 }
    { 42, 420, 66, 666 }
    { "boo", "foo", "goo", "zoo" }
    { [1] = 11, [2] = 22, [3] = "epic", [4] = false }
    { 11, 22, [3] = "epic", [4] = false }
    { 11, 22, [4] = false, [3] = "epic" }
    
    -- The tables below have both array and hash parts
    
    {
        -- Array part
        1,
        22,
        333,
        'yes strings can use single quotes too',
        [5] = 55,
    
        -- Hash part
        ["wow"] = 21,
        lmao = "fun L.U.A. facts",
        [true] = "Yeah, keys can be booleans",
        [6] = 666
    }
    
    {
        -- Hash part
        [0] = "yeah, zero is not array part",
        [-1] = "my key is negative :(",
        [-123456] = "stooooop",
        [6] = "not array part, because [5] is not set",
    
        -- Array part
        101,
        202,
        [3] = 303,
        [4] = 404,
    
        -- Hash part
        [7] = 1337,
        [nil] = "doesn't matter what I put here, it will not exist since the key is nil",
        [8] = 888,
    }
    
    {
        -- Hash part
        name = "test table",
        contenttype = "whatever",
        iscool = true,
    
        -- Array part
        "Hello!",
        "This is some line,",
        "followed by another line."
    }
  4. table.insert, table.remove, table.sort, table.concat and ipairs only work correctly on arrays
    In the context of Lua, "array" refers to any table that is made only of an array part (see fact above).

    In case you wonder, yes, all these functions will inconditionally ignore index 0, because it is part of the hash part, not array part.
    Therefore table.sort will NOT sort the "0th" element of your table.
    Nor will ipairs ever iterate through the 0th element of a table.
    And they will not sort or iterate through any element that comes after a nil "hole".

    To be more accurate, these functions only operate on the array part of the table, so depending on what you're doing, they may still work as expected with non-array tables, but it's usually best to avoid this, as the outcome isn't always easy to predict.
  5. Fact #5: this list is far from complete yet
    And hopefully I will not "forget" to update it.
 
Last edited:
I did noticed right away that Lua is Moon in Portuguese when I first saw it since... You know I'm Portuguese myself
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top